I have this code in my console app, which I'm running to measure the perf impact of using switch-case to map strings to chars:
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
var watch = Stopwatch.StartNew();
HtmlEntities.Lookup("rfloor");
watch.Stop();
Console.WriteLine(watch.Elapsed);
}
}
Here is a gist of the code behind HtmlEntities
, which contains a single method: https://gist.github.com/jamesqo/3331a02f8168a6c782853556ac8f19e5
For some reason when I run the code above, I consistently get a very high number on the first iteration, and then much smaller times on the next ones. For example, here is a sample output to the console after I run this:
00:00:00.0348343
00:00:00.0000016
00:00:00.0000010
00:00:00.0000016
00:00:00.0000016
00:00:00.0000010
00:00:00.0000010
00:00:00.0000016
00:00:00.0000016
00:00:00.0000010
Can anyone explain why this is happening? As far as I can see, the method is just a gigantic switch-case that maps particular strings to chars. Does it have something to do with having to calculate the hash code of the string on the first try, and then on subsequent iterations it is cached?
For comparison, here is an alternative version of HtmlEntities
that initializes a gigantic Dictionary
at runtime and then looks the values up from that. Using that implementation, the first iteration is much much faster, however subsequent iterations are not so fast. Here's a sample output when the app is run using that one:
00:00:00.0000329
00:00:00.0000027
00:00:00.0000021
00:00:00.0000021
00:00:00.0000021
00:00:00.0000021
00:00:00.0000021
00:00:00.0000021
00:00:00.0000021
00:00:00.0000021
Can somebody explain this phenomenon and why it is happening? Thanks.