By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,737 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,737 IT Pros & Developers. It's quick & easy.

ForLoop Performance / Algorithm question

P: n/a
What is the most effective way, fastest way to fill up a String with data?
I wrote the following code, any suggestions on how to improve it?

StringBuffer str = new StringBuffer();
for (int i=0; i<10000; i++)
str.append((char) ((int) ((Math.random() * (254)) + 1) ));

Thanks..
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Użytkownik BlueBall napisał:
What is the most effective way, fastest way to fill up a String with data?
String? With DATA?
I wrote the following code, any suggestions on how to improve it?

StringBuffer str = new StringBuffer();
for (int i=0; i<10000; i++)
str.append((char) ((int) ((Math.random() * (254)) + 1) ));


Then you don't mean character data and you don't want to put it in a
String[1]. What do you *really* want?

[1] it's sooooo C++-ish
--
Ecce Jezuch
"But it's not real and that's why its how I always want to feel
so let's die, before the secret gets revealed
I've tried but nothing ever can appeal
and if you don't mind I'd like to throw it all away" - J. Stem
Jul 17 '05 #2

P: n/a
On Tue, 11 May 2004 17:50:15 GMT, "BlueBall"
<ba*************@hotmail.com> wrote or quoted :
StringBuffer str = new StringBuffer();
for (int i=0; i<10000; i++)
str.append((char) ((int) ((Math.random() * (254)) + 1) ));

try:

Random wheel = new Random();
char [] str = new char[ 10000 ];
for (int i=0; i<10000; i++)
{
str[i] = wheel.nextInt( 255 ) /* 0..254 */ +1 ;
}
// convert to string if you must.
see http://mindprod.com/converter.html

your style of generating random ints can fail sometimes. See
see http://mindprod/jgloss/gotchas.html#RANDOM
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Jul 17 '05 #3

P: n/a
"BlueBall" <ba*************@hotmail.com> wrote in message
news:rn******************@news01.bloor.is.net.cabl e.rogers.com...
What is the most effective way, fastest way to fill up a String with data?
I wrote the following code, any suggestions on how to improve it?

StringBuffer str = new StringBuffer();
for (int i=0; i<10000; i++)
str.append((char) ((int) ((Math.random() * (254)) + 1) ));

Thanks..


char[] c = new char[10000];

// assume JIT is smart
for(int i = 0; i < c.length; i++)
{
c[i] = ((char)(Math.random() * (254)) + 1);
}

String s = new String(c);

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform

Jul 17 '05 #4

P: n/a
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


Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.