Everyone says c++ is faster than c#, why?
-
I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds
#include
#includeusing namespace std;
int main(){
const clock_t beginTime = clock();
for (int i = 2; i < 2000000; i++)
{
bool isPrime = true;
for (int j = 2; j*j <= i; j++)
{
if (i%j == 0){
isPrime = false;
break;
}
}
if (isPrime)
cout << i << endl;
}
cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
}c# is 9 seconds
using System;
namespace Console01
{
class Program
{static void Main(string\[\] args) { DateTime beginTime = DateTime.Now; for (int i = 2; i <= 2000000; i++) { bool isPrime = true; for (int j = 2; j \* j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) Console.WriteLine(i); } TimeSpan ts = DateTime.Now - beginTime; Console.WriteLine(ts.Seconds); } }
}
-
I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds
#include
#includeusing namespace std;
int main(){
const clock_t beginTime = clock();
for (int i = 2; i < 2000000; i++)
{
bool isPrime = true;
for (int j = 2; j*j <= i; j++)
{
if (i%j == 0){
isPrime = false;
break;
}
}
if (isPrime)
cout << i << endl;
}
cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
}c# is 9 seconds
using System;
namespace Console01
{
class Program
{static void Main(string\[\] args) { DateTime beginTime = DateTime.Now; for (int i = 2; i <= 2000000; i++) { bool isPrime = true; for (int j = 2; j \* j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) Console.WriteLine(i); } TimeSpan ts = DateTime.Now - beginTime; Console.WriteLine(ts.Seconds); } }
}
Have you read the Lounge's guidelines which says "No programming questions in the Lounge"? You have to use appropriate forum for your questions; Quick Ansers section would be appropriate in this case.
You always obtain more by being rather polite and armed than polite only.
-
Have you read the Lounge's guidelines which says "No programming questions in the Lounge"? You have to use appropriate forum for your questions; Quick Ansers section would be appropriate in this case.
You always obtain more by being rather polite and armed than polite only.
Personally, I see this more as a technical discussion than a programming question and therefore valid for the Lounge. :^) But I can see why you made the comment. Way more interesting than what is usually in the Lounge. :-\
There are only 10 types of people in the world, those who understand binary and those who don't.
-
Personally, I see this more as a technical discussion than a programming question and therefore valid for the Lounge. :^) But I can see why you made the comment. Way more interesting than what is usually in the Lounge. :-\
There are only 10 types of people in the world, those who understand binary and those who don't.
-
I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds
#include
#includeusing namespace std;
int main(){
const clock_t beginTime = clock();
for (int i = 2; i < 2000000; i++)
{
bool isPrime = true;
for (int j = 2; j*j <= i; j++)
{
if (i%j == 0){
isPrime = false;
break;
}
}
if (isPrime)
cout << i << endl;
}
cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
}c# is 9 seconds
using System;
namespace Console01
{
class Program
{static void Main(string\[\] args) { DateTime beginTime = DateTime.Now; for (int i = 2; i <= 2000000; i++) { bool isPrime = true; for (int j = 2; j \* j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) Console.WriteLine(i); } TimeSpan ts = DateTime.Now - beginTime; Console.WriteLine(ts.Seconds); } }
}
After a quick check, what you're really comparing here is the efficiency of
Console.WriteLine
vscout
If you swapcout
forprintf
you get much more similar times. In my case the C++ was a second faster than the C# but it's not a very fair comparison. Really I don't think you'll find much difference in performance of either. -
I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds
#include
#includeusing namespace std;
int main(){
const clock_t beginTime = clock();
for (int i = 2; i < 2000000; i++)
{
bool isPrime = true;
for (int j = 2; j*j <= i; j++)
{
if (i%j == 0){
isPrime = false;
break;
}
}
if (isPrime)
cout << i << endl;
}
cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
}c# is 9 seconds
using System;
namespace Console01
{
class Program
{static void Main(string\[\] args) { DateTime beginTime = DateTime.Now; for (int i = 2; i <= 2000000; i++) { bool isPrime = true; for (int j = 2; j \* j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) Console.WriteLine(i); } TimeSpan ts = DateTime.Now - beginTime; Console.WriteLine(ts.Seconds); } }
}
Well... you know now how much of a lie it is! Arguably, theoretically, C++ should be faster because it has a better ahead of time compiler. But it's also relying on harder to optimize and write library and less elegant compiler constructs... which make it slower... However C# will slow down at 2 crucial point: - Start-up. C# start-up time is definitely, humanely perceptibly, slower (because of all the assemblies verification) - Allegedly real time code can only be reliably be written in C++ as it is, likely easier to write allocation free code, whereas realtime C# slow down on hidden object allocation (although its allocation algorithm are much faster than C++ ones) - Finally, for laugh, it's just an empty arguments that C++ developer have against C# developer. Surprisingly most of the random comparison code written by random developer make C# wins. But the occasional C++ zealots sometimes come back after a couple of day of profiling and disassembly with a marginally faster C++ version saying that the C++ version was... unoptimized! So... here you have it!
All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!
-
I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds
#include
#includeusing namespace std;
int main(){
const clock_t beginTime = clock();
for (int i = 2; i < 2000000; i++)
{
bool isPrime = true;
for (int j = 2; j*j <= i; j++)
{
if (i%j == 0){
isPrime = false;
break;
}
}
if (isPrime)
cout << i << endl;
}
cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
}c# is 9 seconds
using System;
namespace Console01
{
class Program
{static void Main(string\[\] args) { DateTime beginTime = DateTime.Now; for (int i = 2; i <= 2000000; i++) { bool isPrime = true; for (int j = 2; j \* j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) Console.WriteLine(i); } TimeSpan ts = DateTime.Now - beginTime; Console.WriteLine(ts.Seconds); } }
}
I'm not an expert in this but I can tell you this much: - You didn't set a processor affinity. Stack swaps could have skewed your results. - You didn't set the process and thread priority to high. Your programs code have been interrupted by other threads. - Did you make sure you ran both programs with compiler optimization and without debugger/profiler attached? - Integer-arithmetic isn't representative for the overall speed of the language. Take a look here: Head-to-head benchmark: C++ vs .NET[^]
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
After a quick check, what you're really comparing here is the efficiency of
Console.WriteLine
vscout
If you swapcout
forprintf
you get much more similar times. In my case the C++ was a second faster than the C# but it's not a very fair comparison. Really I don't think you'll find much difference in performance of either.Good catch!
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds
#include
#includeusing namespace std;
int main(){
const clock_t beginTime = clock();
for (int i = 2; i < 2000000; i++)
{
bool isPrime = true;
for (int j = 2; j*j <= i; j++)
{
if (i%j == 0){
isPrime = false;
break;
}
}
if (isPrime)
cout << i << endl;
}
cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
}c# is 9 seconds
using System;
namespace Console01
{
class Program
{static void Main(string\[\] args) { DateTime beginTime = DateTime.Now; for (int i = 2; i <= 2000000; i++) { bool isPrime = true; for (int j = 2; j \* j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) Console.WriteLine(i); } TimeSpan ts = DateTime.Now - beginTime; Console.WriteLine(ts.Seconds); } }
}
You successfully proofed the poor performance of C++ streams. Just try the C++ version with
printf
instead of streams and it should be faster. -
I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds
#include
#includeusing namespace std;
int main(){
const clock_t beginTime = clock();
for (int i = 2; i < 2000000; i++)
{
bool isPrime = true;
for (int j = 2; j*j <= i; j++)
{
if (i%j == 0){
isPrime = false;
break;
}
}
if (isPrime)
cout << i << endl;
}
cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
}c# is 9 seconds
using System;
namespace Console01
{
class Program
{static void Main(string\[\] args) { DateTime beginTime = DateTime.Now; for (int i = 2; i <= 2000000; i++) { bool isPrime = true; for (int j = 2; j \* j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) Console.WriteLine(i); } TimeSpan ts = DateTime.Now - beginTime; Console.WriteLine(ts.Seconds); } }
}
-
I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds
#include
#includeusing namespace std;
int main(){
const clock_t beginTime = clock();
for (int i = 2; i < 2000000; i++)
{
bool isPrime = true;
for (int j = 2; j*j <= i; j++)
{
if (i%j == 0){
isPrime = false;
break;
}
}
if (isPrime)
cout << i << endl;
}
cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
}c# is 9 seconds
using System;
namespace Console01
{
class Program
{static void Main(string\[\] args) { DateTime beginTime = DateTime.Now; for (int i = 2; i <= 2000000; i++) { bool isPrime = true; for (int j = 2; j \* j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) Console.WriteLine(i); } TimeSpan ts = DateTime.Now - beginTime; Console.WriteLine(ts.Seconds); } }
}
Others have already nailed it down to C++ streams. Besides that I remember reading somewhere that the execution on modern highly parallelized systems also highly depends on the structure of the code. For example: modern systems would read several machine instructions ahead and analyze them while executing the current one. So if your code would have multiple jump instructions which would prevent effective pipelining this would probably run slower. Note that I'm not 100% practically sure of what I just wrote. It's theoretical. :)
-
I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds
#include
#includeusing namespace std;
int main(){
const clock_t beginTime = clock();
for (int i = 2; i < 2000000; i++)
{
bool isPrime = true;
for (int j = 2; j*j <= i; j++)
{
if (i%j == 0){
isPrime = false;
break;
}
}
if (isPrime)
cout << i << endl;
}
cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
}c# is 9 seconds
using System;
namespace Console01
{
class Program
{static void Main(string\[\] args) { DateTime beginTime = DateTime.Now; for (int i = 2; i <= 2000000; i++) { bool isPrime = true; for (int j = 2; j \* j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) Console.WriteLine(i); } TimeSpan ts = DateTime.Now - beginTime; Console.WriteLine(ts.Seconds); } }
}
We all know that VB is faster than them both.
cheers Chris Maunder
-
I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds
#include
#includeusing namespace std;
int main(){
const clock_t beginTime = clock();
for (int i = 2; i < 2000000; i++)
{
bool isPrime = true;
for (int j = 2; j*j <= i; j++)
{
if (i%j == 0){
isPrime = false;
break;
}
}
if (isPrime)
cout << i << endl;
}
cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
}c# is 9 seconds
using System;
namespace Console01
{
class Program
{static void Main(string\[\] args) { DateTime beginTime = DateTime.Now; for (int i = 2; i <= 2000000; i++) { bool isPrime = true; for (int j = 2; j \* j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) Console.WriteLine(i); } TimeSpan ts = DateTime.Now - beginTime; Console.WriteLine(ts.Seconds); } }
}
So much is impossible to tell. But, as others have mentioned, avoid for performance. There are some very clever template libraries that do printf like behaviour while retaining type-safety. Which are the best of both worlds. Apart from that, are you timing a release build? C++ typically optimises way more aggressively in release builds.
"If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.
-
Others have already nailed it down to C++ streams. Besides that I remember reading somewhere that the execution on modern highly parallelized systems also highly depends on the structure of the code. For example: modern systems would read several machine instructions ahead and analyze them while executing the current one. So if your code would have multiple jump instructions which would prevent effective pipelining this would probably run slower. Note that I'm not 100% practically sure of what I just wrote. It's theoretical. :)
Tomaž Štih wrote:
I'm not 100% practically sure of what I just wrote.
I'd suggest that as a signature for some of our regular Lounge participants. I won't name names. :^)
There are only 10 types of people in the world, those who understand binary and those who don't.
-
We all know that VB is faster than them both.
cheers Chris Maunder
Shirley you mean VB on Rails!
veni bibi saltavi
-
Have you read the Lounge's guidelines which says "No programming questions in the Lounge"? You have to use appropriate forum for your questions; Quick Ansers section would be appropriate in this case.
You always obtain more by being rather polite and armed than polite only.
Are we that picky now that we cannot even talk about programming language performance in the Lounge ? :confused: If that is the case, then we should rename the site LoungeProject.com ...
I'd rather be phishing!
-
Shall we cut that in half and say it's a programming discussion, or a technical question, then? :) I'm not sure that fits here, anyway.
You always obtain more by being rather polite and armed than polite only.
You sound like a liberal. On and you're wrong. It's simply a technical discussion.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds
#include
#includeusing namespace std;
int main(){
const clock_t beginTime = clock();
for (int i = 2; i < 2000000; i++)
{
bool isPrime = true;
for (int j = 2; j*j <= i; j++)
{
if (i%j == 0){
isPrime = false;
break;
}
}
if (isPrime)
cout << i << endl;
}
cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
}c# is 9 seconds
using System;
namespace Console01
{
class Program
{static void Main(string\[\] args) { DateTime beginTime = DateTime.Now; for (int i = 2; i <= 2000000; i++) { bool isPrime = true; for (int j = 2; j \* j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) Console.WriteLine(i); } TimeSpan ts = DateTime.Now - beginTime; Console.WriteLine(ts.Seconds); } }
}
Not anyone I know says C++ *is* faster than C#. Most of the people I hear talking about it say C++ *can be* faster than C#. It's much easier to play memory arithmetic tricks in C++ than C#. You can do it in C# using Memory Pins and Unsafe blocks, but it's a lot more code to write just to get to a point where you can start doing the actual work of manipulating your values. Also, trivial examples like that won't show you the biggest culprit of performance issues in any .net language: the GC. Once the GC starts grinding away, you can kiss anything that resembles good performance goodbye. You can of course mitigate this by using proper patterns and such, but that's a whole lot of mental overhead that the C++ programmer doesn't need to deal with.
-
We all know that VB is faster than them both.
cheers Chris Maunder
Chris Maunder wrote:
We all know that VB is faster than them both.
But not as fast as Coopers.
Michael Martin Australia "I controlled my laughter and simple said "No,I am very busy,so I can't write any code for you". The moment they heard this all the smiling face turned into a sad looking face and one of them farted. So I had to leave the place as soon as possible." - Mr.Prakash One Fine Saturday. 24/04/2004
-
We all know that VB is faster than them both.
cheers Chris Maunder
Chris Maunder wrote:
We all know that VB is faster than them both.
... I assume this is after you accelerated it out the muzzle of a cannon.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt