1

I would like to declare (and randomize) about 500 strings.

I believe I have two options here:

1: Create an array with a size of 500.

string[] x = new string[500];

for(int i = 0; i < 500; i++)
{
    x[i] = Randomize_thisString(); // Some routine 
}

2: Declare (and randomize) 500 different strings.

string string_one = Randomize_thisString();
string string_two = Randomize_thisString();
string string_n = Randomize_thisString(); 

...

Which one of these would be the faster method, and does anyone know of a third option here?

Thank you,

Evan

  • Option 2 is called a shuffle. You want a Fisher-Yates shuffle, I'm surprised C# doesn't have one built in. See this question: http://stackoverflow.com/questions/375351/most-efficient-way-to-randomly-sort-shuffle-a-list-of-integers-in-c – Mark Ransom Jul 23 '11 at 00:59
  • @Evan, I presume you are worrying about this micro-optimization because you have profiled your app and have identified this section of code as the bottleneck? – Kirk Woll Jul 23 '11 at 01:17
  • 1
    can you do some code for the 2nd option? just to clarify the question a little more – marcelog Jul 23 '11 at 01:35
  • @Kirk - yes this section of my code is somewhat sluggish. Not too bad but yes. –  Jul 23 '11 at 01:40

3 Answers3

2

I will go with first choice, since this will just run once and it not takes much time.
An idea on how create a function to generate the random string I will uses System.IO.Path.GetRandomeFileName() The testing code:

System.IO.Path.GetRandomFileName();

Stopwatch watch = Stopwatch.StartNew();
string[] x = new string[500];

for (int i = 0; i < 500; i++)
{
    x[i] = System.IO.Path.GetRandomFileName();
}

watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds);

result is 2.

Peter K.
  • 8,028
  • 4
  • 48
  • 73
Jalal Said
  • 15,906
  • 7
  • 45
  • 68
  • This is what I ended up using .... almost exactly - even used GetRandomFilename() :). Thank you, I appreciate it. –  Jul 23 '11 at 01:41
0

Just a thought. Not sure if you need all 500 strings upfront.

If you were using this in a loop to do some action, you could use yield to create string only when you need them.

 public IEnumerable<string> GetRandomString()
    {
      for(int i=0; i<500; i++)
       yield return RandomizeString();
    }
coder net
  • 3,447
  • 5
  • 31
  • 40
  • 1
    Khem... Incorrectly updated. The correct return type here is `IEnumerable` where `T` is the return type of `RandomizeString()` function. Presumably it will be `IEnumerable`. – Ivan Danilov Jul 23 '11 at 01:13
  • ugh.. sorry, trust me, I do this almost every week. what a mess :).. also, it was supposed to be an idea, not working code. – coder net Jul 23 '11 at 01:18
  • 1
    @coder, word to the wise, SO users are unsympathetic to non-working code. ;) You might want to consider LinqPAD -- it's a godsend scratchpad for composing one-off coding samples like you encounter on SO. – Kirk Woll Jul 23 '11 at 01:24
  • @coder Yes, I need all 500 strings upfront. Thanks for the response though. –  Jul 23 '11 at 01:32
  • point taken. Just getting a hang of it. Will be more careful in future. – coder net Jul 23 '11 at 01:34
0

What do you mean by

Declare (and randomize) 500 different strings.

string a1 = GenerateRandomString();
string a2 = GenerateRandomString();
string a3 = GenerateRandomString();
.
.
.
.
string a500 = GenerateRandomString();

If so maybe you should revise this lesson

BTW As in both cases program is executed line by line there shouldn't be difference in time required to generate strings

Dan
  • 103
  • 1
  • 1
  • 4
  • Yes, this is what I was referring to –  Jul 23 '11 at 01:31
  • 1
    As I said there wont be any difference in time it will take to generate all strings, however, it doesn't make much sense to write 500 lines of code when there is batter solution to problem – Dan Jul 23 '11 at 01:41