0

Not sure if i phrased the title correctly but say i have a method such as:

public static string[] ByteArrayToStringArray(byte[] buffer)
{
      //code here
}

would it be better to do

List<string> list = new List<string>();

for (int i = 0; i < buffer.Length; i++)
{
    list.Add(buffer[i].ToString());
}
return list.ToArray();

or would it be better to do

List<string> list = new List<string>();

foreach (byte b in buffer)
{
    list.Add(b.ToString());
}
return list.ToArray();

which is faster or is there no difference between them?

Asdar
  • 125
  • 1
  • 5
  • 2
    Or you could just do: `buffer.Select(b => b.ToString()).ToArray();`. – Ron Beyer Feb 24 '18 at 19:40
  • 2
    You really want to convert every single byte into a string? I would guess the whole conversion computation will take so much of the total time that for loop vs foreach loop will hardly make any difference at all… – dumetrulo Feb 24 '18 at 21:18

1 Answers1

0

Like described in this question, iterating an array with a for loop is less cheaper than using a foreach.

You also could improve your code by omitting the list. Just create the array before and set the string items during the loop. Here is the improved version:

public static string[] ByteArrayToStringArray(byte[] buffer)
{
    string[] ret = new string[buffer.Length];
    for (int i = 0; i < buffer.Length; i++)
    {
        ret[i] = buffer[i].ToString();
    }
    return ret;
}

Note that there are also very compact LINQ options like

buffer.Select(c => c.ToString()).ToArray();

but LINQ queries are not best in terms of performance.

EDIT: Referring to the discussion with Gusman at the comments below, it is possible to improve the performance even more by using a while loop.

Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
  • If you're optimizing to the point to distinguish between `for` and `foreach` then would be also reasonalble to say that a `while` loop is faster than a `for` loop. – Gusman Feb 24 '18 at 19:46
  • I don't think so. Looping an array is not faster using a while loop. Check out http://cc.davelozinski.com/c-sharp/for-vs-foreach-vs-while – Fruchtzwerg Feb 24 '18 at 19:48
  • Nice test, but there is not test for `while` loops, check the code. – Gusman Feb 24 '18 at 19:53
  • The result is in the first table of the post. – Fruchtzwerg Feb 24 '18 at 19:56
  • Yes, there is results, but the code for the tests doesn't contain any code to do the tests, that's what I mean, so all that data is just crap. – Gusman Feb 24 '18 at 19:57
  • Hmm ... intresting. Do you have results showing a while loop is faster? – Fruchtzwerg Feb 24 '18 at 19:58
  • https://pastebin.com/wkagMYbT Here you have the test to run by yourself. Also, some results on my computer. As you can see while is always faster, not too much, but noticeable. – Gusman Feb 24 '18 at 20:03
  • Also, all those tests are pure bullshit, according to all that data, ForEach is the fastest of all while the well known reality is that ForEach is the slowest one as it uses IEnumerable. – Gusman Feb 24 '18 at 20:05
  • Looks like you are correct, thanks! – Fruchtzwerg Feb 24 '18 at 20:23