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
The other three are mostly the same. But in general, ToLowerInvariant is fastest, then ToUpper and then ToUpperInvariant .
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.
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.
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.
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:
175
characters only) string just once (not in a loop) should be instant and thus the errors can be enourmousIt 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With