A cross-platform way is to use ftime.
Windows Link: http://msdn.microsoft.com/en-us/library/aa297926(v=vs.60).aspx
An example is below.
#include <stdio.h> #include <sys\timeb.h> int main() { struct timeb start, end; int diff; int i = 0; ftime(&start); while(i++ < 999) { /* do something which takes some time */ printf("."); } ftime(&end); diff = (int) (1000.0 * (end.time - start.time) + (end.millitm - start.millitm)); printf("\nOperation took %u milliseconds\n", diff); return 0; }
I ran the code above and traced it using VS2008 and saw that it actually calls the GetSystemTimeAsFileTime function on Windows.
In any case, ftime will give you millisecond accuracy.
source share