Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ToUpper is faster than ToLower?

Tags:

c#

.net

Stopwatch stopwatch1 = new Stopwatch();
Stopwatch stopwatch2 = new Stopwatch();
string l = "my test";
string u = "MY TEST";

for (int i = 0; i < 25; i++)
{
    l += l;
    u += u;
}

stopwatch1.Start();
l=l.ToUpper();
stopwatch1.Stop();

stopwatch2.Start();
u=u.ToLower();
stopwatch2.Stop();

// Write result.
Console.WriteLine("Time elapsed: \nUPPER :  {0}\n LOWER : {1}",
                  stopwatch1.Elapsed, stopwatch2.Elapsed);

I have run many times:

UPPER : 00:00:01.3386287
LOWER : 00:00:01.4546552

UPPER : 00:00:01.1614189
LOWER : 00:00:01.1970368

UPPER : 00:00:01.2697430
LOWER : 00:00:01.3460950

UPPER : 00:00:01.2256813
LOWER : 00:00:01.3075738
like image 443
Medo Medo Avatar asked Dec 20 '16 08:12

Medo Medo


People also ask

Is ToUpper faster than ToLower?

The other three are mostly the same. But in general, ToLowerInvariant is fastest, then ToUpper and then ToUpperInvariant .

What is the use of ToUpper in C#?

In C#, ToUpper() is a string method. It converts every characters to uppercase (if there is an uppercase version). If a character does not have an uppercase equivalent, it remains unchanged.

Why to use ToLower() in C#?

In C#, ToLower() is a string method. It converts every character to lowercase (if there is a lowercase character). If a character does not have a lowercase equivalent, it remains unchanged.

Does ToLower work on strings?

Strings in JavaScript are immutable. The toLowerCase() method converts the string specified into a new one that consists of only lowercase letters and returns that value.


1 Answers

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:

  1. You have different strings to process
  2. Processing short (175 characters only) string just once (not in a loop) should be instant and thus the errors can be enourmous
  3. 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

like image 135
Dmitry Bychenko Avatar answered Sep 18 '22 01:09

Dmitry Bychenko