Posts Comparing Apples To Apples (Revisited)
Post
Cancel

Comparing Apples To Apples (Revisited)

Like I stated, don't trust someone elses benchmarks. ;-)

Don't get me wrong, in my testing, VB still seems to be a hair faster; but now only by a very slim margin (less than 400ms across all tests).  Please keep in mind that I did not come up with this suite of tests; rather just doing my part to validate the results (since they original results didn't seem to make since to me in the first place).

After going to bed last night, I couldn't help to be bothered by the results I was getting.  How could something that is essentially running very similar IL... even if there are a few minor differences... could be significantly different?  This morning I realized that I wasn't comparing apples to apples. ;-)

The original C# version contained the following code in the test loop for the 32bit integer test:

intResult -= i++;
intResult += i++;
intResult *= i++;
intResult /= i++;

So to translate this to functionally equal code in VB, you'd likely have the following:

intResult -= i : i += 1
intResult += i : i += 1
intResult *= i : i += 1
intResult \= i : i += 1

There the same, right?  As it turns out, there are significant differences between them in IL.  By changing the methods using this pattern of code in the C# benchmark, there was a measurable performance increase.  Looking at the IL you'll see that using the original code the dup operator (which copies the current topmost value on the evaluation stack, and then pushes the copy onto the evaulation stack) is being utilized; whereas the modified code works directly on the value in the stack.

The other flaw in my apples to apples was that I left the C# code alone for the IO testing.  In VB I utilized a For/Next loop instead of a while loop and adding to an integer value until it reached the maximum number.  I modified the C# version to use the same as the VB version.

After modifying these, the tests are much closer together in regards to performance and look more like what I was originally expecting.

As Jason asked, I've attached the code so the skeptics can do as I've done... play with it and see what you can find.

[download]

This post is licensed under CC BY 4.0 by the author.