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); } }
}
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
-
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
-
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.
I already have it for long time :-D ;P
M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.
-
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); } }
}
C++ is filth. For real speed you need C. :cool:
-
C++ is filth. For real speed you need C. :cool:
That depends a lot on whether you want to reuse components such as sort algorithms. I doubt very much you can match std::sort performance with C code, except for hand-coding every damn sort. Even then, C++ optimisation is damn fine nowadays.
"If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.
-
That depends a lot on whether you want to reuse components such as sort algorithms. I doubt very much you can match std::sort performance with C code, except for hand-coding every damn sort. Even then, C++ optimisation is damn fine nowadays.
"If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.
Rob Grainger wrote:
hand-coding every damn sort
As appropriate for the specific task, yes.
-
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); } }
}
This link is a blog series done by Raymond Chen and Rico Mariani where Raymond wrote a Chinese dictionary in C++ and Rico wrote a naïve line-by-line translation of the C++ code in C#. The C# code blew away the C++ code in performance. Raymond then did a number of ever more severe optimizations (including writing his own string class) to finally beat the naïve C# performance. Rico then optimized the C# code and again beat the C++ performance. Raymond then did a couple of more optimizations to finally beat the C#. The summary is thus: Doing a straightforward implementation in C# will yield really good performance (and is much quicker to implement, relative to C++), in many cases better than a straightforward implementation in C++. But, if you put in a significant amount of optimization effort, C++ CAN clearly outperform the C# code - as noted in the final blog post of that series, there's certain initial overhead (60ms in Rico's case) that the CLR imposes that you cannot optimize away in C#.
-
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); } }
}
Rule #1 of bench-marking code like this is to make damn sure both pieces of code are on the same playing field. As you've already been told, they are not. NEVER EVER EVER put output code (WriteLine, cout, printf, setting a TextBox.Text property, ...) inside your timing code. If you need to print result strings, put the result values in a plain old, preallocated(!), array. Arrays works pretty much the same across all languages so there isn't much of an overhead difference between implementations. Output your data to the screen after you exit the timing code. This way, you're not timing the efficiency of the output code on top of your algorithm code. As you've seen, the differences between console, stream and even visual control implementations can be HUGE.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
Rule #1 of bench-marking code like this is to make damn sure both pieces of code are on the same playing field. As you've already been told, they are not. NEVER EVER EVER put output code (WriteLine, cout, printf, setting a TextBox.Text property, ...) inside your timing code. If you need to print result strings, put the result values in a plain old, preallocated(!), array. Arrays works pretty much the same across all languages so there isn't much of an overhead difference between implementations. Output your data to the screen after you exit the timing code. This way, you're not timing the efficiency of the output code on top of your algorithm code. As you've seen, the differences between console, stream and even visual control implementations can be HUGE.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave KreskowiakDave Kreskowiak wrote:
NEVER EVER EVER
:laugh: Hmmm, sounds like you're waffling on this issue.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle