In UNIX/UNIX-like OSes, you can use the time command.
I'm trying to implement that in Windows, and my first quick attempt this :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[]){
clock_t start,end;
int x;
char command[100];
if (argc > 1){
for (int x = 1; x < argc; x++){
strcat(command,argv[x]);
strcat(command," ");
}
start = clock();
system(command);
printf("%f\n",(end-start)/CLOCKS_PER_SEC);
end = clock();
}
return 0;
}
The last output is always "0.0000", which means the execution time is not captured properly.
How can I fix that ? Maybe some Win32 API needed ?
...
start = clock();
system(command);
end = clock();
printf("%f\n",(float)(end-start)/CLOCKS_PER_SEC);
...
GetTickCount() works great for me.
Ah, silly mistake :P
I made some workarounds, and the code works :
timeit.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[]){
clock_t start,end;
int x;
char command[100];
if (argc > 1){
strcpy(command," ");
for (x = 1; x < argc; x++){
strcat(command,argv[x]);
strcat(command," ");
}
start = clock();
system(command);
end = clock();
printf("%f\n",(float)(end-start)/CLOCKS_PER_SEC);
}
return 0;
}
Now if I invoke the program, e.g :
timeit python matrixmultiplication.py 10, it will print the number estimated to be the running time
But i think system() has overhead
Any better solution ?
Quote from: andre104 on October 30, 2008, 08:35:28 AM
But i think system() has overhead
Any better solution ?
I have an old program, that i found many years ago and which i ported to Pelles C. It starts other programs without system. Perhaps it is a better solution, perhaps not - look at it. ;-)
Quote from: andre104 on October 30, 2008, 08:35:28 AM
But i think system() has overhead
Any better solution ?
The overhead to system() is so small that you shouldn't really care about it. Unless you're timing operations that are so trivial they execute faster than a call to system.