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

Paolo Ferraresi <>
Thu, 5 Aug 2021 18:24:48 -0000 (UTC)

          From comp.compilers

Related articles
| List of all articles for this month |

From: Paolo Ferraresi <>
Newsgroups: comp.compilers
Date: Thu, 5 Aug 2021 18:24:48 -0000 (UTC)
Organization: NNTP Server
Injection-Info:; posting-host=""; logging-data="48390"; mail-complaints-to=""
Keywords: question, performance, comment
Posted-Date: 05 Aug 2021 15:02:00 EDT

Hello, my name is Paolo Ferraresi and I program in both C# and C++, for
(sorry for my bad English but I will never learn properly)
I like both C# and C++. I have no preclusions of the religion wars type. I
find C# very convenient for almost any application but C++ should be
considered when maximum efficiency and performance is required.
Since a few days I'm on vacation and a bit for fun, I wrote a few lines
that make the sieve of Eratosthenes.
It never happens to me to write exactly the same program for C# and for C+
+, but so for fun I said to myself: - I write two equivalent codes,
without .NET and STL containers, only predefined data and arrays, without
library algorithms, only for cycles on arrays.

Here is the C# code:
using System;
using System.Diagnostics;
class Program
        const uint N = 2147483591; //Maximum array size in C#;
        static void Main(string[] args)
                Stopwatch sw = new Stopwatch();
                bool[] A = new bool[N];
                for (uint i = 2; i < N; ++i) A[i] = true;
                for (uint i = 2; i < N; ++i)
                        if (A[i])
                                for (uint j = i; i * j < N; ++j)
                                A[i*j] = false;

                Console.WriteLine("Tempo impiegato {0} ms",
                Console.Write("Premi un tasto... ");

Here is the C++ code:
#include <iostream>
#include <iterator>
#include <array>
#include <vector>
#include <chrono>
#include <algorithm>
using namespace std;
int main()
        const unsigned int N = 2147483591;
        auto Tstart = chrono::high_resolution_clock::now();
        bool* A = new bool[N];
        fill(A, A + N, true);
        for (unsigned int i = 2; i < N; ++i)
                if (A[i])
                        for (unsigned int j = i; i * j < N; ++j)
                                A[i * j] = false;
        auto Tend = chrono::high_resolution_clock::now();
        chrono::duration<double, std::milli> diff = Tend - Tstart;
        cout << "Tempo impiegato " << diff.count() << "ms\n";
        delete[] A;
        cout << "Press ENTER ";
        return 0;

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.

If any of you would like to try it, then explain what's not working at my
place? Thanks bye!
Greetings from Italy! :)

Paolo Ferraresi
[I would guess the difference is something unrelated to the loops, such as how the
two runtime systems allocate a two gigabyte array. -John]

Post a followup to this message

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