Re: Not normal for the same program to be faster in C# than in C++ [Visual Studio 2019]

George Neuner <>
Thu, 05 Aug 2021 22:58:02 -0400

          From comp.compilers

Related articles
| List of all articles for this month |

From: George Neuner <>
Newsgroups: comp.compilers
Date: Thu, 05 Aug 2021 22:58:02 -0400
Organization: A noiseless patient Spider
References: 21-08-001
Injection-Info:; posting-host=""; logging-data="82082"; mail-complaints-to=""
Keywords: performance
Posted-Date: 06 Aug 2021 09:41:36 EDT

On Thu, 5 Aug 2021 18:24:48 -0000 (UTC), Paolo Ferraresi
<> wrote:
  a C# program
  a C++ program
>I understand very well that the validity of such a game is almost null,
>but since I remain convinced that the same code (and I repeat the same
>almost 1:1, not that one used arrays and the other STL) cannot be faster
>in C# than in C++, imagine the surprise when the results came out:
>C# (release build): 23093 ms, (48630 ms in debug build).
>C++(release build): 33516 ms, (44906 ms in debug build).
>I came to the conclusion that maybe I have a specific problem from me and
>not from others. I mean apart from the numerical values, which will be
>different depending on the hardware each of us has, try to see if C++
>turns out faster from you, which is what I expect, honestly.
>Also I changed build from debug to release and that's it, leaving
>everything default, except that I always put x64 platform.
>Finally, I use Windows 10 Pro, Visual Studio 2019 community edition and as
>I mentioned the code was compiled for x64 platform.
>The CPU is AMD Ryzen Threadripper 3970X.
>[I would guess the difference is something unrelated to the loops, such as how the
>two runtime systems allocate a two gigabyte array. -John]

My guess is that the issue is how (and what) you are timing.

Multitasking operating systems royally screw up attempts to accurately
time things. If you want to compare code, you should run many
iterations of each version and compare their /average/ running times.

As John mentioned, you are timing allocation of the array. Heap
management is very different in these two languages, so the time to
allocate things is, in general, not comparable.

You shouldn't time the array initialization either unless you do it
the same way in both programs. The templated fill() algorithm in C++
will not necessarily be equivalent to the inline C# code - it depends
on your compiler settings. [see below]

I would modify your programs like so (in pseudo):

    total = 0
    allocate array
    for N iterations
          initialize array
          start = current time
                run the seive
          stop = current time
          total += (stop - start)
    average = total / N

And then run for N = 50 (or more) to filter out multitasking related
noise in the individual timings.

To really be fair you need to find out what optimizations are being
done by the C# and dotNET JIT compilers (which work together), and
adjust your C++ compiler to do the equivalent. Simply doing a
'release' compile in both languages is not sufficient: in general C++
is harder to optimize than C#, and many of the possible optimizations
are disabled by default because they can break code that does not
comply with their requirements. Except in 'unsafe' code, C# largely
makes it impossible for code to not comply with its optimization

But start with more accurate timing.

Hope this helps,

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.