Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The key elements in human thinking are not numbers but labels of fuzzy sets. -- L. Zadeh


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

SubjectAuthor
* Not normal for the same program to be faster in C# than in C++ [Visual Studio 20Paolo Ferraresi
`* Re: Not normal for the same program to be faster in C# than in C++ [Visual StudiGeorge Neuner
 `* Re: Not normal for the same program to be faster in C# than in C++ [Visual StudiDmitry A. Kazakov
  `- Re: Not normal for the same program to be faster in C# than in C++ [Visual Studigah4

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

<21-08-001@comp.compilers>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=72&group=comp.compilers#72

 copy link   Newsgroups: comp.compilers
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.snarked.org!border2.nntp.dca1.giganews.com!nntp.giganews.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end
From: fp....@alice.it (Paolo Ferraresi)
Newsgroups: comp.compilers
Subject: Not normal for the same program to be faster in C# than in C++ [Visual Studio 2019]
Date: Thu, 5 Aug 2021 18:24:48 -0000 (UTC)
Organization: Aioe.org NNTP Server
Lines: 92
Sender: news@iecc.com
Approved: comp.compilers@iecc.com
Message-ID: <21-08-001@comp.compilers>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970";
logging-data="48390"; mail-complaints-to="abuse@iecc.com"
Keywords: question, performance, comment
Posted-Date: 05 Aug 2021 15:02:00 EDT
X-submission-address: compilers@iecc.com
X-moderator-address: compilers-request@iecc.com
X-FAQ-and-archives: http://compilers.iecc.com
 by: Paolo Ferraresi - Thu, 5 Aug 2021 18:24 UTC

Hello, my name is Paolo Ferraresi and I program in both C# and C++, for
passion/study.
(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();
sw.Start();
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;
sw.Stop();

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

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 ";
cin.get();
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
fp.box@alice.it
[I would guess the difference is something unrelated to the loops, such as how the
two runtime systems allocate a two gigabyte array. -John]

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

<21-08-003@comp.compilers>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=73&group=comp.compilers#73

 copy link   Newsgroups: comp.compilers
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!usenet.csail.mit.edu!news.iecc.com!.POSTED.news.iecc.com!nerds-end
From: gneun...@comcast.net (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
 by: George Neuner - Fri, 6 Aug 2021 02:58 UTC

On Thu, 5 Aug 2021 18:24:48 -0000 (UTC), Paolo Ferraresi
<fp.box@alice.it> 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

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

<21-08-005@comp.compilers>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=74&group=comp.compilers#74

 copy link   Newsgroups: comp.compilers
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!usenet.csail.mit.edu!news.iecc.com!.POSTED.news.iecc.com!nerds-end
From: mail...@dmitry-kazakov.de (Dmitry A. Kazakov)
Newsgroups: comp.compilers
Subject: Re: Not normal for the same program to be faster in C# than in C++ [Visual Studio 2019]
Date: Fri, 6 Aug 2021 16:14:22 +0200
Organization: Aioe.org NNTP Server
Lines: 38
Sender: news@iecc.com
Approved: comp.compilers@iecc.com
Message-ID: <21-08-005@comp.compilers>
References: <21-08-001@comp.compilers> <21-08-003@comp.compilers>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970";
logging-data="72327"; mail-complaints-to="abuse@iecc.com"
Keywords: performance
Posted-Date: 08 Aug 2021 11:14:14 EDT
X-submission-address: compilers@iecc.com
X-moderator-address: compilers-request@iecc.com
X-FAQ-and-archives: http://compilers.iecc.com
Content-Language: en-US
 by: Dmitry A. Kazakov - Fri, 6 Aug 2021 14:14 UTC

On 2021-08-06 04:58, George Neuner wrote:

> 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

Another technique is factoring out looping and other overheads by
running empty loop as a reference:

start = current time
for N iterations
initialize array
run the sieve
end loop;
total1 = start - current time

start = current time
for N iterations
initialize array
end loop;
total2 = start - current time

average = (total1 - total2) / N -- sieve only

P.S. Optimizations is a usual suspect of ruining benchmark measures.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

<21-08-008@comp.compilers>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=76&group=comp.compilers#76

 copy link   Newsgroups: comp.compilers
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.imp.ch!usenet.csail.mit.edu!news.iecc.com!.POSTED.news.iecc.com!nerds-end
From: gah...@u.washington.edu (gah4)
Newsgroups: comp.compilers
Subject: Re: Not normal for the same program to be faster in C# than in C++ [Visual Studio 2019]
Date: Sun, 8 Aug 2021 12:38:51 -0700 (PDT)
Organization: Compilers Central
Lines: 30
Sender: news@iecc.com
Approved: comp.compilers@iecc.com
Message-ID: <21-08-008@comp.compilers>
References: <21-08-001@comp.compilers> <21-08-003@comp.compilers> <21-08-005@comp.compilers>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970";
logging-data="14123"; mail-complaints-to="abuse@iecc.com"
Keywords: performance, comment
Posted-Date: 08 Aug 2021 16:01:54 EDT
X-submission-address: compilers@iecc.com
X-moderator-address: compilers-request@iecc.com
X-FAQ-and-archives: http://compilers.iecc.com
In-Reply-To: <21-08-005@comp.compilers>
 by: gah4 - Sun, 8 Aug 2021 19:38 UTC

On Sunday, August 8, 2021 at 8:14:17 AM UTC-7, Dmitry A. Kazakov wrote:

(snip)

> P.S. Optimizations is a usual suspect of ruining benchmark measures.

Yes. But in this case, one might want to include some optimizations.

Note, though, that a good optimizer could optimize out all the loops, as no
output depends on them. Some programs I know output the total number
of primes found, which stops that from happening.

Also, compilers can do some calculations at compile time. I don't expect
it for this, but that does ruin some benchmarks. There are stories of complicated
benchmarks being done entirely at compile time, except for output of the result.

I would have used a j += i loop. Not that multiply is that slow on modern processors,
but that it a big part of the loop. One compiler might optimize that one for you.

One might store the array as bits (8 bool/byte), the other as bytes. It isn't so
obvious which one is faster, but often the 1 bool/byte is faster, until you run out
of real memory.

How much real memory do you have? And the speed might depend in complicated
ways on the memory management system.

And note that you aren't comparing languages, but two compilers implementing
those languages (which is why it goes here).
[In this case, the documentation says they both allocate a byte for each bool but the other
stuff is all possible. Also remember C++ is a traditional compiler, while C# is bytecode and JIT. -John]

1
server_pubkey.txt

rocksolid light 0.9.7
clearnet tor