473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ForLoop Performance / Algorithm question

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
4 2009
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: Carl | last post by:
"Nine Language Performance Round-up: Benchmarking Math & File I/O" http://www.osnews.com/story.php?news_id=5602 I think this is an unfair comparison! I wouldn't dream of developing a numerical...
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
3
by: Roland | last post by:
Hi! I am working on a project in which i implement a mathematical optimization algorithm. One of the requirements on the code is very good performance. So i started to optimize a bit. Now i do...
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
26
by: pembed2003 | last post by:
Hi, I have an application where I use the strncmp function extensively to compare 2 string to see if they are the same or not. Does anyone know a faster replacement for strncmp? I notice there is...
3
by: pertheli | last post by:
Hello, I have a large array of pointer to some object. I have to run test such that every possible pair in the array is tested. eg. if A,B,C,D are items of the array, possible pairs are AB, AC,...
18
by: koorb | last post by:
Every so often I read something like: "use a defensive code technique like File.Exists instead of a Try and Catch because it's less of a performance hog" And when I program, I obviously have...
4
by: deanhiller | last post by:
I have read alot on the fact that a large program could potentially run faster in C++ than C due to the fact that it can be organized better/designed better. I can't seem to redig up these...
7
by: David Veeneman | last post by:
This is a very simple question, but for moe reason I can't find an answer. Do I take a performance hit if a foreach statement has to evaluate an expression? For example, consider the following:...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.