Not that it doesn't work. In fact, it does. But it is not the right way to measure time as the clock ()
function returns an approximation of processor time used by the program. I am not sure about other platforms, but on Linux you should use clock_gettime ()
with CLOCK_MONOTONIC
flag - that will give you the real wall time elapsed. Also, you can read TSC, but be aware that it won't work if you have a multi-processor system and your process is not pinned to a particular core. If you want to analyze and optimize your algorithm, I'd recommend you use some performance measurement tools. I've been using Intel's vTune for a while and am quite happy. It will show you not only what part uses the most cycles, but highlight memory problems, possible parallelism issues etc. You may be very surprised with the results. For example, most of the CPU cycles might be spent waiting for memory bus. Hope it helps!
UPDATE: Actually, if you run later versions of Linux, it might provide CLOCK_MONOTONIC_RAW
, which is a hardware-based clock that is not a subject to NTP adjustments. Here is a small piece of code you can use: