BlueBall wrote:
StringBuffer str = new StringBuffer();
for (int i=0; i<10000; i++)
str.append((char) ((int) ((Math.random() * (254)) + 1) ));
Leaving aside the weirdness of what you are doing...
1) Create the StringBuffer with the necessary capacity in advance:
StringBuffer str = new StringBuffer(10000);
Tony Morris's suggestion might well be even faster, though, providing you don't
mind creating the extra array.
2) Do you /really/ need the data to be so random ? You may be happy with:
StringBuffer str = new StringBuffer(10000);
char[] c = new char[100];
for (int i = 0; i < c.length; i++)
c[i] = ((char)(Math.random() * (254)) + 1);
for (int i = 0; i < 100; i++)
str.append(c);
which generates a lot less random data, and so should be faster (I haven't
measured it).
3) Come to that, do you need /random/ data at all ? Perhaps you could just
fill in c[] (above) with:
for (int i = 0; i < c.length; i++)
c[i] = (char)i;
But I still think this is a really strange thing to want to do. Or at least to
want to do /and/ care how fast it is...
-- chris