[SOLVED] Performance/profiling measurement in C


I’m doing some prototyping work in C, and I want to compare how long a program takes to complete with various small modifications.

I’ve been using clock; from K&R:

clock returns the processor time used by the program since the beginning of execution, or -1 if unavailable.

This seems sensible to me, and has been giving results which broadly match my expectations. But is there something better to use to see what modifications improve/worsen the efficiency of my code?

Update: I’m interested in both Windows and Linux here; something that works on both would be ideal.

Update 2: I’m less interested in profiling a complex problem than total run time/clock cycles used for a simple program from start to finish—I already know which parts of my program are slow. clock appears to fit this bill, but I don’t know how vulnerable it is to, for example, other processes running in the background and chewing up processor time.


In POSIX (e.g. on Linux), you can use gettimeofday() to get higher-precision timing values (microseconds).

In Win32, QueryPerformanceCounter() is popular.

Beware of CPU clock-changing effects, if your CPU decides to clock down during the test, results may be skewed.

Answered By – unwind

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *