RE: [Dev-C++] Linux vs Windows speed
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ho...> - 2002-08-19 23:13:42
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf Of jgana
> Sent: Monday, August 19, 2002 8:16 PM
> To: dev...@li...
> Subject: [Dev-C++] Linux vs Windows speed
>
>
> First, thanks to everybody for your help
> Second, forgive me, if I am asking something stupid.You know, I am
> learning.
>
> I found out that a console program that is compiled with
> Dev-C++ (Mingw)
> can run perfectly well in Linux and a lot faster. The program
> runs ~70%
> slower in Windows.
> Why there is a difference in speed if I am using the same machine?
> (Pentium 400 MHz)
>
> The program that I used is a very inefficient way to calculate the
> Fibonacci Series. But, the point was to have a rough idea of
> the speed.
> The Dev-C++ (Mingw) executable in windows took 1 minute 45 seconds in
> finding the 22th element of the Fibonacci Series (17,711). The same
> executable in Linux took 30 seconds to find the 22th element.
In windows try compiling using:
-fexpensive-optimizations -O3 -ffloat-store -mcpu=pentiumpro
>
> #include <iostream.h>
#include <iostream>
> #include <stdlib.h>
Or better: #include <cstdlib>
>
> int Fib(int n);
>
> int main()
> {
> using namespace std;
> int n, answer, stop;
>
> cout <<"Enter number to find: ";
> cin >>n;
> cout <<"\n\n";
> answer=Fib(n);
> cout << answer <<" is the " << n <<"th Fibonacci number\n";
>
> cin >>stop;
Or better: cin.ignore();
> return 0;
> }
>
> int Fib(int n)
> {
using std::cout;
> cout <<"Processing Fib(" << n << ")...";
> if (n<3)
> {
> cout <<"return 1!\n";
> return (1);
> }
> else
> {
> cout <<"call Fib(" << n-2 <<") and Fib(" << n-1 << ").\n";
> return( Fib(n-2)+Fib(n-1));
> }
> }
The most important: Printing on the screen introduces tremendous time
cost. Probably it is more expensive in Windows than Linux. So rremove
all those couts from Fib(). This will make the benchmark more accurate.
Finally as you know using a loop is faster than using recursion. Also
recursion occupies more & more space in the stack, and for large
"depths" you will be without space in the stack. Loop has not this
problem too.
But this is left as an excercise for you. :)
Finally, although these drawbacks, i can't leave this code as it is
without taking advantage of the power of C++.
With the addition of a time measuring mechanism, the removal of of
those couts from inside Fib(), and making the Fib() inline we have the
code:
#include <iostream>
#include <cstdlib>
#include <ctime>
inline int Fib(int n)
{
using namespace std;
if (n<3)
return (1);
else
return( Fib(n-2)+Fib(n-1));
}
int main()
{
using namespace std;
int n, answer;
cout <<"Enter number to find: ";
cin >>n;
cout <<"\n\n";
clock_t t1=clock();
answer=Fib(n);
cout << answer <<" is the " << n <<"th Fibonacci number\n";
clock_t t2=clock();
cout<<"It took "<<t2-t1<<". Translated in seconds: ";
cout<<static_cast<double>(t2-t1)/CLOCKS_PER_SEC<<endl;
cin.ignore();
return 0;
}
Compile it with the optimisation switches and tell me the results. :)
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|