Have you ever considered how well a regular if/else-statement performs? I haven’t, but after watching this Channel9 video I wanted to test it.

In the video, Brian Beckman explains that the order of the statements is important to the performance. In other words; the most likely statement to succeed should be the first. Let’s take a look at a simple method with an if/else statement. As you can see, it doesn’t do much, but that’s exactly what we want in order to test the if/else-statement.

  private bool RunIf(string input)

  {

    if (input == "hello")

      return true;

    else if (input == "jelly")

      return true;

    else

      return true;

  }

If the theory is correct, it should be faster to pass in “hello” than “jelly”, which again would be faster than a random third string. To test this, we need a method that calls RunIf() a lot of times in order to measure it.
 

  private void Test()

  {

    DateTime start = DateTime.Now;

 

    for (int i = 0; i < 100000000; i++)

    {

      RunIf("hello");

    }

 

    TimeSpan span = DateTime.Now.Subtract(start);

    Console.Write(span.TotalMilliseconds);

  }

Results

I did three tests. In the first I passed “hello” to the RunIf, the second “jelly” and the third “other” – all 5 letter words for consistency. Here’s the total runtime in milliseconds for each of the three tests.

1. “hello”: 1546 milliseconds
2. “jelly”: 3687 milliseconds
3. “other”: 4453 milliseconds

It is actually more than twice as fast to run the first if-statement as having to move into the “if else” statement.

So, what can we use this information for? Probably not much, because I had to make a hundred million iterations in the Test() method just to be able to measure the result. Unless of course you have an application that does just that, then this is merely a little information for your consideration.

Or maybe we can go as far as to say that it would improve the performance of your if/else-statements to run the first if-statement more often than the else statement.

Working in the web marketing statistics business, I know how difficult it is to produce reliable statistics about visitors and their actions. A good web stat application, whether it is behavioral or marketing related, has to make good decisions all the time to produce correct, uniform, and reliable statistics. It takes constant monitoring and adjustments and also has to be build with intelligence for self maintenance. It also has to make decisions about fraud and various browsers and platforms. To summarize; it’s a very, very difficult discipline.

Despite all these efforts to produce uniform and reliable statistics I just found that some of the biggest free statistic engines do not agree by far. Here on this website, I use StatCounter and Google Analytics to give me a simple visitor behavior statistic. The reason why I use both of them is because I am curious to how each of them performs and their ability to give me the correct statistic.

Because they produce the same kind of web statistics in a very similar manor, I expected them to produce the same reports more or less. However, this is not the case – not by a long shot.

Google Analytics counts 28% lesser unique visitors than StatCounter. 28%!! This is not a minor fluke in The Matrix, but a massive inconsistency. The problem is that neither of them tells me why. Whether it’s because Google Analytics has an elaborate fraud filter or because the StatCounter script is below Google’s in the HTML, I don’t know. But I do know that it leaves me rather confused and makes me question who of them is right, if any of them are. I don’t want to analyze my log files, that’s too much of a trouble and I don’t like trouble. Besides, who says my log files are correct. They also have to be cleansed for fraud, spiders and other clients and request that are irrelevant.

So, where does this leave me hanging? As I see it, I need a third web stat on my page and compare them all. If this doesn’t do the trick, a fourth and a fifth will be put to action. If you know any correct, uniform and reliable web stat, please let me know. Remember, it has to be free for this experiment.