| | |
C# faster than C++ ??
![]() |
Hey Everyone,
I'm doing a little test for a presentation I'm giving on Tuesday. I wanted to show the performance difference between VB2008, C# and C++.
To do this, I have three programs. Each program performs the Fibonacci sequence X times, and can either do it iteratively or recursively. As expected both C# and C++ blow VB away. However, my test between C# (with GUI) and C++ (command line) was a little surprising.
I did 40 Fibonacci numbers, both iteratively and recursively. The iterative tests show the same time to the millisecond almost. However, when doing the recursive test, C# completed the calculation in 3.8 seconds, while it took C++ about 12.1 seconds. Is this expected? I would have thought C++, especially with it being command line, would have outperformed C#?
Here are my codes... maybe you can see an 'unfairness' that might help clear things up. Or am I wrong in thinking C++ is quicker than C#?
C++ Code:
C# Code:
Thanks
I'm doing a little test for a presentation I'm giving on Tuesday. I wanted to show the performance difference between VB2008, C# and C++.
To do this, I have three programs. Each program performs the Fibonacci sequence X times, and can either do it iteratively or recursively. As expected both C# and C++ blow VB away. However, my test between C# (with GUI) and C++ (command line) was a little surprising.
I did 40 Fibonacci numbers, both iteratively and recursively. The iterative tests show the same time to the millisecond almost. However, when doing the recursive test, C# completed the calculation in 3.8 seconds, while it took C++ about 12.1 seconds. Is this expected? I would have thought C++, especially with it being command line, would have outperformed C#?
Here are my codes... maybe you can see an 'unfairness' that might help clear things up. Or am I wrong in thinking C++ is quicker than C#?
C++ Code:
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <ctime> #include <stdio.h> #include <time.h> using namespace std ; unsigned long first = 0; unsigned long second = 1 ; unsigned long result ; time_t start, end ; double total ; int x ; int ans ; unsigned long fib( int n ) ; int main() { cout << endl << " Fibonacci Sequence Test -> C++ " << endl ; cout << "_____________________________________" << endl ; cout << endl << endl << endl ; cout << "Number of iterations to calculate: " ; cin >> x ; cout << endl << endl ; cout << "1. Iterative" << endl ; cout << "2. Recursive" << endl ; cout << "Select method: " << endl ; cin >> ans ; cout << endl ; if ( ans == 1 ) { start = clock() ; for ( int i = 0; i <= x ; i++ ) { //cout << result << endl ; first = second ; second = result ; result = first + second ; } end = clock() ; total = (end - start) * .001 ; cout << "Result: " << endl ; cout.precision(10) ; cout << "Total Time: " << total << endl ; } else if ( ans == 2 ) { start = clock() ; result = fib(x) ; end = clock() ; total = (end - start) * .001 ; cout << "Result: " << result << endl ; cout.precision(10) ; cout << "Total Time: " << total << endl ; } return 0 ; } unsigned long fib( int n ) { if (n <= 1) return n; else return fib(n-1)+fib(n-2); };
C# Code:
c# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace FibCsharp { public partial class Form1 : Form { double startTime; double endTime; double totalTime; ulong first = 0; ulong second = 1; ulong result; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { startTime = DateTime.Now.Hour * 3600 + DateTime.Now.Minute * 60 + DateTime.Now.Second + DateTime.Now.Millisecond * .001; for (int i = 1; i <= 40; i++) { first = second; second = result; result = first + second; } endTime = DateTime.Now.Hour * 3600 + DateTime.Now.Minute * 60 + DateTime.Now.Second + DateTime.Now.Millisecond * .001; totalTime = endTime - startTime; label1.Text = Convert.ToDouble((totalTime)).ToString(); textBox1.Text = result.ToString(); } private void button2_Click(object sender, EventArgs e) { startTime = DateTime.Now.Hour * 3600 + DateTime.Now.Minute * 60 + DateTime.Now.Second + DateTime.Now.Millisecond * .001; result = fib(40); endTime = DateTime.Now.Hour * 3600 + DateTime.Now.Minute * 60 + DateTime.Now.Second + DateTime.Now.Millisecond * .001; totalTime = (endTime - startTime); label1.Text = Convert.ToDouble(totalTime).ToString(); textBox1.Text = result.ToString(); } private ulong fib(ulong n) { if (n <= 1) return n; else return fib(n - 1) + fib(n - 2); } } }
Thanks
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.
-Edsger Dijkstra
-Edsger Dijkstra
Let me start by telling I am sort of a C# affictionado, with intrests in other languages. I am as surprised as you are. Even more because you did extra calculations in the C#version to time the thing.
Perhaps that's a problem. Try to use if possible the same class for timing. I don't know for C++ but in C# you have the Timer classes and the StopWatch class. An example of the use of StopWatch can be found in http://www.daniweb.com/code/snippet979.html.
Perhaps that's a problem. Try to use if possible the same class for timing. I don't know for C++ but in C# you have the Timer classes and the StopWatch class. An example of the use of StopWatch can be found in http://www.daniweb.com/code/snippet979.html.
Last edited by ddanbe; Nov 2nd, 2008 at 4:29 am. Reason: typo
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
Firstly, your iterative versions aren't equivalent either: your iterate over a different interval in the loop.
As to the differences with the recursive versions, there are all sorts of possible explanations.
1) The way you're measuring time may have different granularity. The clock time is not infinite resolution - hence the tip by ddanbe to do a large number of calculations: for small duration calculations the error in the computed interval may be larger than the actual interval i.e. your timing results may be meaningless because you're not doing enough calculations.
2) The conversion of an interval (measured with two calls of clock()) is generally required to be divided by the value of the macro CLK_TCK to convert the interval to seconds. You're not doing that, so it's possible you've introduced a scaling factor on the computed times.
3) Compiler optimisation settings can have a significant effect: for example comparing a "Release" build with a "Debug" build in the other languages. "Release" builds tend to be optimised for speed, but "Debug" versions are not.
My guess, in your case, is a combination of all three.
As to the differences with the recursive versions, there are all sorts of possible explanations.
1) The way you're measuring time may have different granularity. The clock time is not infinite resolution - hence the tip by ddanbe to do a large number of calculations: for small duration calculations the error in the computed interval may be larger than the actual interval i.e. your timing results may be meaningless because you're not doing enough calculations.
2) The conversion of an interval (measured with two calls of clock()) is generally required to be divided by the value of the macro CLK_TCK to convert the interval to seconds. You're not doing that, so it's possible you've introduced a scaling factor on the computed times.
3) Compiler optimisation settings can have a significant effect: for example comparing a "Release" build with a "Debug" build in the other languages. "Release" builds tend to be optimised for speed, but "Debug" versions are not.
My guess, in your case, is a combination of all three.
The VC++ now defines CLOCKS_PER_SEC (old CLK_TCK) macros as 1000, so there are no bad scaling in that case. However a real measured interval (i.e. VC++ RTL clock() precision) is about 15 milliseconds, so it's impossible to measure short intervals (iterative variant with clock() ticks <2 MICROseconds!).
For example, look at my snippet based on CPU Performance Clock Counter API (~1 microsecond precision):
http://www.daniweb.com/code/snippet916.html
Release version of this test runs 1.3 seconds on my CPU (debug version ~8 milliseconds). So I think recursive fib() code compiled with VC++ 2008 (and others) is faster than its C# incarnation, that's not the point.
Let's look at the "yellow press style" post header. The question: what's an object of this test? Is it C++, or C#? Obviously, answer negative. It's a test of 331,169,281 function calls. Of course, function call overheads is a very important parameter of - what? The language? No, it's a parameter of the language implementation (compiler+RTL).
It's a well-known fact: there are different compilers with different code generator qualities. Visual C++ is not a champion of C++ code optimisation.
So be careful with post titles
...
PS. It does not matter GUI or console environment used for function calls...
For example, look at my snippet based on CPU Performance Clock Counter API (~1 microsecond precision):
http://www.daniweb.com/code/snippet916.html
Release version of this test runs 1.3 seconds on my CPU (debug version ~8 milliseconds). So I think recursive fib() code compiled with VC++ 2008 (and others) is faster than its C# incarnation, that's not the point.
Let's look at the "yellow press style" post header. The question: what's an object of this test? Is it C++, or C#? Obviously, answer negative. It's a test of 331,169,281 function calls. Of course, function call overheads is a very important parameter of - what? The language? No, it's a parameter of the language implementation (compiler+RTL).
It's a well-known fact: there are different compilers with different code generator qualities. Visual C++ is not a champion of C++ code optimisation.
So be careful with post titles
...PS. It does not matter GUI or console environment used for function calls...
Last edited by ArkM; Nov 2nd, 2008 at 8:36 am.
•
•
Join Date: Nov 2008
Posts: 3
Reputation:
Solved Threads: 0
Here is my results for exactly your code in my Dell Inspiron 6400 laptop with Intel T2500 CPU and 2gb of Ram.
Results for VC++ 9.0
Results for Intel C++ compiler 10
And the result for C# 3 with .Net framework 3.5
As you can see, in my computer test runs in C++ about %31 faster than C#.
Perhaps you forgot to enable optimization when compiling with C++.
PS the post title isn't appropriate.
Results for VC++ 9.0
C++ Syntax (Toggle Plain Text)
Fibonacci Sequence Test -> C++ _____________________________________ Number of iterations to calculate: 40 1. Iterative 2. Recursive Select method: 2 Result: 102334155 Total Time: 2.5 Press any key to continue . . .
Results for Intel C++ compiler 10
C++ Syntax (Toggle Plain Text)
Fibonacci Sequence Test -> C++ _____________________________________ Number of iterations to calculate: 40 1. Iterative 2. Recursive Select method: 2 Result: 102334155 Total Time: 2.391 Press any key to continue . . .
And the result for C# 3 with .Net framework 3.5
C++ Syntax (Toggle Plain Text)
Best Result for C#3 and .Net3.5 was 3.562
As you can see, in my computer test runs in C++ about %31 faster than C#.
Perhaps you forgot to enable optimization when compiling with C++.
PS the post title isn't appropriate.
Just for the record, I wasn't trying to do yellow journalism or anything of that sort. If the title is offensive somehow, I apologize. Actually, I expected and was rooting for C++, so I assure you I wasn't trying to make false accusations. If a Mod would like to change the title to something more appropriate, feel free. Doesn't make a difference to me.
Thanks for the replies. I can understand datetime and clock() causing a timing difference in the iterative versions, but not the recursive; not that big anyways. The differences in time was about 8 seconds. I'm sure the different timing methods didn't cause an 8 second delay.
The iteration tests were the same to whoever said they weren't (as far as the number of iterations). I posted the last code I used, but I made sure to change it before my tests so that they were fair between the two.
I wasn't aware of an optimization capability. Thanks. Where do I access that? Also, is there the same sort of capability for C#?
thanks again for everyone's help
Thanks for the replies. I can understand datetime and clock() causing a timing difference in the iterative versions, but not the recursive; not that big anyways. The differences in time was about 8 seconds. I'm sure the different timing methods didn't cause an 8 second delay.
The iteration tests were the same to whoever said they weren't (as far as the number of iterations). I posted the last code I used, but I made sure to change it before my tests so that they were fair between the two.
I wasn't aware of an optimization capability. Thanks. Where do I access that? Also, is there the same sort of capability for C#?
thanks again for everyone's help
Last edited by Duki; Nov 2nd, 2008 at 6:45 pm.
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.
-Edsger Dijkstra
-Edsger Dijkstra
•
•
Join Date: Nov 2008
Posts: 3
Reputation:
Solved Threads: 0
By default all optimizations are enabled in the release configuration in visual studio standard edition and above. Remember that, all optimizations are off in default Debug configuration.
You can access optimization settings in project property page in the C/C++->Optimization and in the linker->optimization sections.
And if you are using cl command line, these switches are related to optimization:
/Od
/O1
/O2
/Ox
/Oi
and for linker:
/OPT:
Consult MSDN for more information.
You can access optimization settings in project property page in the C/C++->Optimization and in the linker->optimization sections.
And if you are using cl command line, these switches are related to optimization:
/Od
/O1
/O2
/Ox
/Oi
and for linker:
/OPT:
Consult MSDN for more information.
Last edited by rdjusr; Nov 3rd, 2008 at 6:33 pm.
![]() |
Similar Threads
- How to make Windows XP 200 % faster??? (Windows NT / 2000 / XP)
- how do you make java programs faster? (Java)
- Faster graphics (Graphics and Multimedia)
- Use AutoComplete to Enter Addresses Faster in Internet Explorer 6 (Windows tips 'n' tweaks)
- Shutdown XP Faster (Windows tips 'n' tweaks)
- Help me make my boot time faster (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Conversion error C2440
- Next Thread: Another beginner who needs to get one a foot in.
Views: 1827 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm array arrays assignment basic beginner binary c++ c++borland c/c++ calculator char class classes client code compile compiler console constructor conversion convert count delete dll dynamic encryption error file files forms fstream function functions game givemetehcodez graph gui homework iamthwee input int lazy link linker list loop loops map math matrix member memory multidimensional network newbie number numbers object objects opengl output pointer pointers problem program programming project qt random read recursion recursive reference return search sort spoonfeeding string strings struct student studio system template templates text time tree undefined variable vc++ vector video visual win32 window windows winsock wxwidgets






