Compare execution time

Hello I want to compare the execution time between this 2 codes:
I get a 0.007 to 0.035s , I did not get a clear result of execution time in code::blocks compiler, its always different
the recursive one
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include <iostream>
using namespace std;
int fac (int a)
{
    if(a>1)
    return ( a*(fac(a-1)));//
  else//if 0
    return(1);//if 0 entered or
}
int main ()
{
    cout<<fac(5)<<endl;
  return 0;
}

the classic one
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
int main()
{
    int fac=5, answer=1;

    for(int cnt=1;cnt<=fac;cnt++)
         answer=answer*cnt;

    cout<<answer<<endl;
}


the inline function one
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include <iostream>
using namespace std;
inline int fac (int a)
{
    if(a>1)
    return ( a*(fac(a-1)));//
  else//if 0
    return(1);//if 0 entered or
}
int main ()
{
    cout<<fac(5)<<endl;
  return 0;
}
Last edited on
You can't inline a recursive function....

EDIT:

Also, I don't see any time measuring code. How are you clocking this?

And I assume you have optimizations turned on? If not, the numbers are totally meaningless.
Last edited on
Nothing really, its just in Code::block you get the execution time automatically with out having to calculate it

http://img837.imageshack.us/img837/8871/bv4d.png

I think it can be calculated using <time.h> time_t timer; & using __TIME__ for start of execution
but when I use the inline function, it gets recursive in my compiler, I think the compiler is ignoring the command or what?

http://en.wikipedia.org/wiki/Inline_function

"Without inline functions, however, the compiler decides which functions to inline."
3:40 AM, enough coding for me for today, good night.
Nothing really, its just in Code::block you get the execution time automatically with out having to calculate it


You cannot reliably benchmark code that way, as it is timing everything including process startup and shutdown... both of which are likely taking 100x longer than your simple factorial code.

You really need to surround the code you want to time in some kind of time measuring mechanism. Something like this:

1
2
3
4
5
6
7
8
9
int main()
{

  start_timer();

  // .. the code you want to time

  stop_timer();
}


Use whatever timer you want, but a high frequency timer would be more precise and reliable. If you have C++11, the <chrono> header probably has timers you can use. I'd have to research it myself =x




Also, based on the very small snapshot of the compiler options in that image... I do not see optimizations enabled. You absolutely must enable optimizations to do any kind of benchmarking or your numbers will be completely unrealistic.

Many IDEs come with 2 premade "configurations" called 'Debug' and 'Release'. I think C::B has this as well. Make sure you switch it to Release and rebuild. That will enable optimizations.


EDIT:

Also... you really need to run the code many, many, many times. Like at least several thousand. Just running it once will be so fast that the time it takes will be way too small to measure with any kind of precision. So really, something like this would be better:

1
2
3
4
5
6
7
8
9
10
11
int main()
{
    start_timer();

    for(int i = 0; i < 10000; ++i)
    {
        // ... code you want to time
    }

    stop_timer();
    // divide the time taken by 10000 to find out how long 1 iteration takes on average. 


when I use the inline function, it gets recursive in my compiler, I think the compiler is ignoring the command or what?


If you don't have optimizations turned on, it's definitely ignoring the command.

I suppose the compiler might be able to inline a recursive function if you're calling it with a constant (which you are in this case), so I might have been wrong when I said you couldn't do that.



ANOTHER EDIT:
Also, since you're not using the output of your factorial code, it's possible the compiler will optimize it away completely, resulting in your time measurement being practically 0.


you are using the output. Nevermind. Still, with heavy inlining and optimizations it's very possible the compiler will optimize this:
 
cout<<fac(5)<<endl;

into this:
 
cout << 120 << endl;


In which case the only thing you'd be measuring is cout speed and not factorial speed.
Last edited on
Thank you, but I think I am using the debug and release function when I make the program as a "project file"
but this program is just a .cpp file compiled into .exe
but this program is just a .cpp file compiled into .exe


Err... well that doesn't matter.

If you're using an IDE, look for a Debug/Release option.

If you're not using an IDE, look up the commandline options for whatever compiler you're using and give it the options to enable optimizations. I believe it's -O2 for gcc.

ie: g++ myfile.cpp -O2 or something like that. I practically never invoke compilers manually so I'd have to look up exactly how to do it.
You mean this?
Command line macro :
$compiler $options $includes -c $file -o $object
add -02?


I tried to google it yesterday, now now again, I did not find how exactly to do it.
I am still a beginner you know, and started using code::blocks 3 days ago
You mean this?


If that's a makefile, you'd want to add it to the $options macro rather than add it directly to the final commandline string. That's what the "options" macro is for... specifying the compile options.

EDIT: and like I said, I'm not sure it's -O2. You'll want to double check that by looking at your compiler's documentation. It's probably easier to just use an IDE like C::B.

started using code::blocks 3 days ago


Well if you're using C::B all you have to do is switch it to Release.
Last edited on
I will read the documentation, but I never had the time since I just read my c++ book & the news every day

Here is the -O2

http://img24.imageshack.us/img24/2236/gkf.png
for speed it says
and one more thing, I noticed that an .exe file made with codeblocks is ~900KP while in dev cpp is ~500kp

so the optimization has something to do with it as I think
Last edited on
Topic archived. No new replies allowed.