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:
#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:
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