Get precise time (performance counter)


long perf



Returns the number of microseconds since Windows startup. Useful to measure code speed.


When high precision is not necessary, you can instead use GetTickCount (~15 ms precision) or timeGetTime (2 ms precision). They return the number of milliseconds since Windows startup.


When measuring code speed, it is a good idea to repeat it several times. Your thread may be interrupted to give CPU time to other programs and threads that need it. For example, when you move the mouse, OS needs some time to move the mouse pointer. Also, code speed depends on various CPU and other hardware features.


Note: Depending on computer speed, perf itself may get 1 or several microseconds.

Note: The type of the return value is long (64-bit), not int (32-bit). Be careful when displaying the results formatted: either at first assign the difference to an int variable, or in the format-string use %I64i instead of %i, or use F-string.


See also: PerfFirst, PerfNet, PerfOut



 measure how long runs code
long t1=perf ;;get time before
int i j
for i 0 1000
long t2=perf ;;get time after
out t2-t1 ;;display the difference