Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is C# performance slower than VB

Tags:

c#

vb.net

I have two library, first one is the original which was done in vb.net, second one is in c#.

doing exactly the same thing.

vb.net is about 10% faster than c# which is very strange

so what I have found that seem the cause of the slowdown, by looking at IL code of both is(i would say that close to 99% of the il code is the same);

  1. in c# all method call have hidebysig but not in vb.net

    is this one thing that could be a performance issue?

  2. in c# you must initialize a local variable before using it

    this wont work in c#

    void test()
    {
        int a;
        a += 1;
    }
    

    this will

    void test()
    {
        int a = 0;
        a += 1;
    }
    

    while this work in vb.net

    Sub test()
       Dim a As Integer
       a += 1
    End Sub
    

    which in c# cause 2 more IL line which, I'm pretty sure, cause a performance issue

  3. in vb.net it seem I cannot get the il code to use call, it always use callvirt while c# always use call

    is this one thing that could be a performance issue?

  4. .maxstack is sometime bigger in c#

    is this one thing that could be a performance issue?

in the end, I'm trying to understand how to get back that 10% speed loss. So far I'm clueless

if you want to take a look here it is, you can decompile it yourself, i used ilspy;

ZIP file, compiled version
ChessEngine.dll
ChessEngineSharp.dll
ConsoleApplication1.exe

like image 701
Fredou Avatar asked Apr 09 '26 00:04

Fredou


2 Answers

hidebysig just controls how name lookup in overridden methods works.

which in c# cause 2 more IL line which, I'm pretty sure, cause a performance issue

No, it doesn’t. The same IL should be produced – or equivalent code. In VB initialisation is mandatory, it’s just that the compiler does it implicitly for you if you don’t do it explicitly.

in vb.net it seem I cannot get the il code to use call, it always use callvirt while c# always use call

I’m pretty sure you got that the wrong way round. C# will always use callvirt on virtual method, VB supports call by using the MyClass.Method() syntax.

In fact, if your benchmark shows that VB is 10% faster then I suspect there’s an error in your benchmark, nothing more.

like image 110
Konrad Rudolph Avatar answered Apr 11 '26 12:04

Konrad Rudolph


I've tried running it a few times, but the timings on my system don't show any particular bias.

I tried running the "BenchVB" and "BenchC#" tests four times, with these results:

    BenchVB, BenchC#
    Average Moves per Second
C#: 49,218,819    48,975,863    47,096,647    47,796,195
VB: 47,003,681    46,874,143    49,137,566    49,382,133

Sometimes C# is faster, sometimes VB is faster. It doesn't look like there's any significant differences, at least on my PC (quad core running at 4GHz, Windows 7 x64).

like image 36
Matthew Watson Avatar answered Apr 11 '26 13:04

Matthew Watson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!