Path: csiph.com!xmission!usenet.csail.mit.edu!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: George Neuner Newsgroups: comp.compilers Subject: Re: Not normal for the same program to be faster in C# than in C++ [Visual Studio 2019] Date: Thu, 05 Aug 2021 22:58:02 -0400 Organization: A noiseless patient Spider Lines: 79 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <21-08-003@comp.compilers> References: <21-08-001@comp.compilers> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 8bit Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="82082"; mail-complaints-to="abuse@iecc.com" Keywords: performance Posted-Date: 06 Aug 2021 09:41:36 EDT X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: csiph.com comp.compilers:2691 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 requirements. But start with more accurate timing. Hope this helps, George