Let's try reproducing the result
// Please, notice: the same string for both ToUpper/ToLower
string GiniPig = string.Concat(Enumerable
.Range(1, 1000000) // a million chunks "my test/MyTest" combined (long string)
.Select(item => "my test/MY TEST"));
Stopwatch sw = new Stopwatch();
// Let's try n (100) times - not just once
int n = 100;
var sampling = Enumerable
.Range(1, n)
.Select(x => {
sw.Reset();
sw.Start();
GiniPig.ToLower(); // change this into .ToUpper();
sw.Stop();
return sw.ElapsedMilliseconds; })
.ToSampling(x => x); // Side library; by you may save the data and analyze it with R
Console.Write(
$"N = {n}; mean = {sampling.Mean:F0}; std err = {sampling.StandardDeviation:F0}");
Having run several times (warming) I've got the results (Core i7 3.6 GHz, .Net 4.6 IA-64):
ToLower: N = 100; mean = 38; std err = 8
ToUpper: N = 100; mean = 37; std err = 9
So you can't reject the null hypothesis that ToLower
is as faster as ToUpper
and thus your experiment has got errors:
- You have different strings to process
- Processing short (
175
characters only) string just once (not in a loop) should be instant and thus the errors can be enourmous
- You have to warm up the routine (in order methods to be compiled, assemblies loaded, caches filled up etc.)
It seems (the time elapsed is more than 1 second for a very easy operation) it's rule #3 (warming up) breakage which ruined the experiment