Skip to content

Commit

Permalink
Improve CPU usage of random string generation
Browse files Browse the repository at this point in the history
  • Loading branch information
toddlipcon committed Apr 25, 2010
1 parent 3c69a05 commit dccfa38
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/com/yahoo/ycsb/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ public class Utils
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);
}

/**
Expand Down

0 comments on commit dccfa38

Please sign in to comment.