Fredou
Fredou

Reputation: 20140

why is C# performance slower than VB

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

Upvotes: 3

Views: 1534

Answers (3)

Fredou
Fredou

Reputation: 20140

I'm going to say that my issue seem to be hardware dependent, at home VS at work is day and night and the only difference is intel desktop cpu(home) vs intel mobile cpu(work). (at work, c# is faster than vb...)

I wish at home the number would have been more close/similar between c# / vb

Upvotes: 0

Matthew Watson
Matthew Watson

Reputation: 109822

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).

Upvotes: 2

Konrad Rudolph
Konrad Rudolph

Reputation: 546073

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.

Upvotes: 8

Related Questions