So I was thinking about what's more efficient, making a number by directly adding them as a string to each other, or string.Formatting them.
So I made a little code piece:
private static void measuringStuff()
{
Stopwatch stw = new Stopwatch();
Random random = new Random();
string a;
stw.Start();
for (int i = 0; i < 10000000; i++)
{
a = random.Next(10) + "" + random.Next(10);
}
stw.Stop();
Console.WriteLine("#+\"\"+#: " + stw.ElapsedMilliseconds);
stw.Reset();
stw.Start();
for (int i = 0; i < 10000000; i++)
{
a = string.Format("{0}{1}", random.Next(10), random.Next(10));
}
stw.Stop();
Console.WriteLine("string.Format: " + stw.ElapsedMilliseconds);
}
And it turns out:
# + "" + #: 1928
string.Format: 2667
Why?