Calculate time

Printable View

• 05-16-2009
Goo
Calculate time
Hi

I’m looking for a method that can calculate the time that it takes to export a piece of code, i.e. the speed like - O(N)

Something like this (I know this is Java code, but I need it in C++):

Code:

```QTimer t = new QTimer(); t.start(); //now a method that calls a method for each tick, eg. for each tick “t”, we call graph.redraw(). //calculations t.stop();```

Can someone please help me?
• 05-16-2009
tabstop
You can look in the header <ctime> to see what's available.
• 05-16-2009
matsp
O(N) can not be measured trivially by measuring time. O(N) for one algorithm could be 1000x times slower than anohter O(N) algorithm. All O(N) tells you is that if you double the number of elements, it takes twice as long - but it actually says NOTHING about the amount of time to perform the operations in themselves.

Consider an two implementations sorting strings, where one implementation is copying the entire string, another copies just a pointer. For longer strings, that would make a big difference.

As to measuring time precisely, you may want to look at clock(). However, that is a bit dependant on the OS and what your application is doing - Windows gives the amount of time passed, whilst in Linux/Unix it gives the amount of actual CPU time. It makes little difference if the CPU is 100% busy all the time. But if the program does for a lot of for example file I/O, it may not use the CPU 100%, so the number you get will be different.

--
Mats