Sunday, July 12, 2009

C# change array of strings into one string.?

Hi, in the software I'm developing (nothing professional, just as a hobby, I'm only 16), I have an array of strings, and sometimes the array can be as large as 1 million. Each string in the array holds 10 characters. At the moment the program has to work through the array like this:


string total = null;


int x = 0;


while (x %26lt; myArray.Length)


{


total = total + myArray[x];


x++;


}





This takes upto half an hour with my processor (it's a 3.0GHz dual core, but I realise only one core is being used).





Is there a quicker way to do what I'm doing? If so, how?





Thanks, Thomas





BTW if you want to try the software, it can be found here:





www.wormalds.co.uk/ThomasWormaldEncryp...

C# change array of strings into one string.?
Well, I don't know of any shortcut method to what you're doing. Maybe this setup might run faster, but then again, maybe not.





string[] what = {"So", " what", " if", " I", " have", " this?"};





string newAns = null;





foreach(string ans in what)


newAns += ans;





If this doesn't work out to be any better, then its time to use threads. You mentioned that only one core is being used. You could break up the task and do some of the work in different threads. However, you'd have synchronize the efforts and merge everything back into once string at the end, so you'll have to wait for each thread to complete its task.
Reply:Why the hell would you have an an array of one million strings? What possible practical use does that have?


No comments:

Post a Comment