3

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

Fredou
  • 19,848
  • 10
  • 58
  • 113

3 Answers3

8

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.

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • I added a link to the end of my question with the compiled version, you can take a look with ilspy or anything else – Fredou Mar 24 '13 at 19:15
  • i'm pretty sure the benchmark is good since the stopwatch is outside of the library – Fredou Mar 24 '13 at 19:19
  • I don’t have ILSpy (I’m on OS X / Mono) but I’m still certain about my assessment of the benchmark. Just using `Stopwatch` doesn’t mean anything, there are plenty of possibilities for errors to slip in. To start with: did you consider JIT timing, did you do a statistical analysis with significance testing, did you look at memory access patterns / GC activity? – Konrad Rudolph Mar 24 '13 at 19:22
  • even if i run it 20 times in a row c# is slower than vb.net, 5 to 10% difference – Fredou Mar 24 '13 at 19:30
  • @Fredou That would be normal for a systematic error and in itself isn’t a sign that the benchmark is correct. – Konrad Rudolph Mar 24 '13 at 20:17
2

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

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • 1
    on mine I'm getting 49-51 millions on c# and 53-54 millions on my vb.net, so far, c# was never equal or faster than vb.net which i find odd – Fredou Mar 24 '13 at 20:59
0

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

Fredou
  • 19,848
  • 10
  • 58
  • 113