4

I'm at the beginning with C++ and sometimes I don't know how much my compiler will like two different implementation of an alghoritm. Is there a simple tool I can use to see how much time takes my code to execute?

EDIT: Im using gcc compiler

doplumi
  • 2,938
  • 4
  • 29
  • 45

3 Answers3

7

Free

Not Free

Xunie
  • 437
  • 4
  • 21
Caesar
  • 9,483
  • 8
  • 40
  • 66
2

I have had very good experience with AQtime from Smart Bear, it is not free, but you can get a free trial. It integrates really well into Visual C++ and RAD Studio from Embarcardero.

http://smartbear.com/products/qa-tools/application-performance-profiling

The data is easily accessible in the IDE, and especially the hit count and time spent numbers in the gutter section, next to the line numbers is useful.

Tommy Andersen
  • 7,165
  • 1
  • 31
  • 50
2

If you want to mesure how long the entire program run's, then Code-Blocks/Visual studio should tell you when the program closes. It should be in the log at the bottom.

If you want to mesure how long a specific line, or function takes, I would suggjest researching clock() or QueryPerformanceFrequency() and how to use them.

the clock() function is slow, but it's easyer to use. an example:

float start_time = clock()/CLOCKS_PER_SEC;
func();
float end_time = clock()/CLOCKS_PER_SEC;
float dtime = start_time - end_time;
Wolfgang Skyler
  • 1,338
  • 1
  • 13
  • 26
  • 2
    Just bear in mind that `clock` does something different on Windows than on Unix (Linux, MacOS, etc). It makes little difference if your code is just plain code, but if you do I/O or otherwise cause the system to run something else, in Windows, `clock` gives the total time the code ran, where in Unix it gives the CPU-time consumed by the program - so something doing 10 seconds of file-I/0 may come out as `0.02s`, because that's how much of the time was actually the CPU-time, but it took 10 seconds "real time". – Mats Petersson Mar 01 '13 at 23:18