The trouble is that standard C functions like time() and clock() do not have very good precision, as you have discovered. Sometimes 1/18.2 of a second accuracy; sometimes only 1 second accuracy.
To get higher granularity, try Windows functions like QueryPerformaceCounter(). (I think that's the one.) http://support.microsoft.com/kb/q172338/
[edit] Example:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <windows.h>
LARGE_INTEGER stime;
void start_time(void);
long end_time(void);
int main(int argc, char *argv[]) {
char *call = 0, *p;
size_t lcall = 0, len;
int x;
if(argc <= 1) {
printf("\nusage: timeit command_to_time [args]\n");
return 1;
}
for(x = 1; x < argc; x ++) {
len = strlen(argv[x]);
p = realloc(call, lcall+len+1 + (x+1 < argc ? 1 : 0));
if(!p) {
fprintf(stderr, "Out of memory\n");
free(call);
return 1;
}
call = p;
strcpy(call+lcall, argv[x]);
if(x+1 < argc) {
call[lcall+len] = ' ';
call[lcall+len+1] = 0;
lcall ++;
}
lcall += len;
}
start_time();
system(call);
printf("%li\n", end_time());
free(call);
return 0;
}
void start_time(void) {
QueryPerformanceCounter(&stime);
}
long end_time(void) {
LARGE_INTEGER diff;
LARGE_INTEGER freq;
QueryPerformanceCounter(&diff);
diff.QuadPart -= stime.QuadPart;
diff.QuadPart *= 1000; /* Adjust to milliseconds, shouldn't overflow */
QueryPerformanceFrequency(&freq);
return (long)(diff.QuadPart / freq.QuadPart);
}
That's a Windows program I wrote a long time ago to time how long any arbitrary command takes to execute. It's not very well coded. (Note the missing <string.h>.) The times also do not take into account how slow system() is, but it's an okay example. Just ignore everything in main(). [/edit]