I tested the speed of i.ToString()
, i + ""
, Console.WriteLine(i+"")
and Console.WriteLine(i)
. (i
was an int
)
The results were the following:
i.ToString()
(149 ms) was faster than i + ""
(202 ms).
But when I used Console.WriteLine
Console.WriteLine(i + "")
(743 ms) was a lot faster than Console.WriteLine(i)
(927 ms, according to TextWriter source Console.WriteLine calls ToString()
internally).
Now my question is: Why is i + ""
faster in combination with Console.WriteLine()
?
Notes:
- I used 1,000,000 iterations for the non-console stuff and 1,000 for my console tests.
- I use .NET Framework 4.7.1
- The code was compiled with Debug configuration in Visual Studio 2017 (version 15.9.4). Release gives similar results.
- The project type is console application.
- Code is available as a GitHub Gist