Last active
May 10, 2018 09:11
-
-
Save rudiv/fc63e09df6a9df0bce8dfcd64bf2a5b9 to your computer and use it in GitHub Desktop.
string comparison tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Faster (probably due to no debugging attached by roslyn/linqpad), but same results overall | |
ICIC - 2.0579534 | |
ToLower - 3.2817216 | |
ToUpper - 3.2320578 | |
ToLowerInvariant - 2.7057271 | |
ToUpperInvariant - 2.7467674 | |
*/ | |
using System; | |
namespace Benchmark | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var sw = new System.Diagnostics.Stopwatch(); | |
sw.Start(); | |
for (int i = 0; i < 10000000; i++) | |
{ | |
var valid = string.Equals(("test" + i), "test", StringComparison.InvariantCultureIgnoreCase); | |
} | |
"ICIC".Dump(); | |
sw.Elapsed.TotalSeconds.Dump(); | |
sw.Reset(); | |
sw.Start(); | |
for (int i = 0; i < 10000000; i++) | |
{ | |
var valid = ("test" + i).ToLower() == "test".ToLower(); | |
} | |
"ToLower".Dump(); | |
sw.Elapsed.TotalSeconds.Dump(); | |
sw.Reset(); | |
sw.Start(); | |
for (int i = 0; i < 10000000; i++) | |
{ | |
var valid = ("test" + i).ToUpper() == "test".ToUpper(); | |
} | |
"ToUpper".Dump(); | |
sw.Elapsed.TotalSeconds.Dump(); | |
sw.Reset(); | |
sw.Start(); | |
for (int i = 0; i < 10000000; i++) | |
{ | |
var valid = ("test" + i).ToLowerInvariant() == "test".ToLowerInvariant(); | |
} | |
"ToLowerInvariant".Dump(); | |
sw.Elapsed.TotalSeconds.Dump(); | |
sw.Reset(); | |
sw.Start(); | |
for (int i = 0; i < 10000000; i++) | |
{ | |
var valid = ("test" + i).ToUpperInvariant() == "test".ToUpperInvariant(); | |
} | |
"ToUpperInvariant".Dump(); | |
sw.Elapsed.TotalSeconds.Dump(); | |
Console.ReadLine(); | |
} | |
} | |
public static class Ext | |
{ | |
public static void Dump(this object obj) | |
{ | |
Console.WriteLine(obj.ToString()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Optimisations Off - | |
Run 1 - | |
ICIC - 2.3968469 | |
ToLower - 4.068176 | |
ToUpper - 4.1186162 | |
ToLowerInvariant - 3.0551849 | |
ToUpperInvariant - 3.0872983 | |
Run 2 - | |
ICIC - 2.4894112 | |
ToLower - 3.9751046 | |
ToUpper - 3.8831133 | |
ToLowerInvariant - 2.9753864 | |
ToUpperInvariant - 2.9298081 | |
Run 3 - | |
ICIC - 2.3738692 | |
ToLower - 3.965096 | |
ToUpper - 3.9167838 | |
ToLowerInvariant - 3.0479106 | |
ToUpperInvariant - 3.1812765 | |
Optimisations On - | |
Run 1 - | |
ICIC - 2.3813724 | |
ToLower - 3.8624087 | |
ToUpper - 3.7243602 | |
ToLowerInvariant - 2.7946187 | |
ToUpperInvariant - 2.7731418 | |
Run 2 - | |
ICIC - 2.3676069 | |
ToLower - 3.7968581 | |
ToUpper - 3.7123926 | |
ToLowerInvariant - 2.7868444 | |
ToUpperInvariant - 2.7985568 | |
Run 3 - | |
ICIC - 2.3024725 | |
ToLower - 3.7179663 | |
ToUpper - 3.6960497 | |
ToLowerInvariant - 2.7884367 | |
ToUpperInvariant - 2.7717545 | |
*/ | |
var sw = new System.Diagnostics.Stopwatch(); | |
sw.Start(); | |
for(int i = 0; i < 10000000; i++) | |
{ | |
var valid = string.Equals(("test" + i), "test", StringComparison.InvariantCultureIgnoreCase); | |
} | |
"ICIC".Dump(); | |
sw.Elapsed.TotalSeconds.Dump(); | |
sw.Reset(); | |
sw.Start(); | |
for(int i = 0; i < 10000000; i++) | |
{ | |
var valid = ("test" + i).ToLower() == "test".ToLower(); | |
} | |
"ToLower".Dump(); | |
sw.Elapsed.TotalSeconds.Dump(); | |
sw.Reset(); | |
sw.Start(); | |
for(int i = 0; i < 10000000; i++) | |
{ | |
var valid = ("test" + i).ToUpper() == "test".ToUpper(); | |
} | |
"ToUpper".Dump(); | |
sw.Elapsed.TotalSeconds.Dump(); | |
sw.Reset(); | |
sw.Start(); | |
for(int i = 0; i < 10000000; i++) | |
{ | |
var valid = ("test" + i).ToLowerInvariant() == "test".ToLowerInvariant(); | |
} | |
"ToLowerInvariant".Dump(); | |
sw.Elapsed.TotalSeconds.Dump(); | |
sw.Reset(); | |
sw.Start(); | |
for(int i = 0; i < 10000000; i++) | |
{ | |
var valid = ("test" + i).ToUpperInvariant() == "test".ToUpperInvariant(); | |
} | |
"ToUpperInvariant".Dump(); | |
sw.Elapsed.TotalSeconds.Dump(); | |
// .equals 00:00:02.3217525 vs == 00:00:03.8858363 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment