Skip to content
Browse files

Improve CPU usage of random string generation

  • Loading branch information...
1 parent 3c69a05 commit ed39c1107e62c8a7666421ef7a1ed2bddbfa903b @toddlipcon toddlipcon committed with brianfrankcooper Apr 26, 2010
Showing with 11 additions and 9 deletions.
  1. +11 −9 src/com/yahoo/ycsb/Utils.java
View
20 src/com/yahoo/ycsb/Utils.java
@@ -32,15 +32,17 @@
public static String ASCIIString(int length)
{
int interval='~'-' '+1;
-
- StringBuilder str=new StringBuilder();
- for (int i=0; i<length; i++)
- {
- char c=(char)(random.nextInt(interval)+' ');
- str.append(c);
- }
-
- return str.toString();
+
+ byte []buf = new byte[length];
+ random.nextBytes(buf);
+ for (int i = 0; i < length; i++) {
+ if (buf[i] < 0) {
+ buf[i] = (byte)((-buf[i] % interval) + ' ');
+ } else {
+ buf[i] = (byte)((buf[i] % interval) + ' ');
+ }
+ }
+ return new String(buf);
}
/**

0 comments on commit ed39c11

Please sign in to comment.
Something went wrong with that request. Please try again.