Skip to content

Instantly share code, notes, and snippets.

@alsamitech
Created April 23, 2021 08:26
Show Gist options
  • Select an option

  • Save alsamitech/fd766d263afebbf58b57fabb80d44a00 to your computer and use it in GitHub Desktop.

Select an option

Save alsamitech/fd766d263afebbf58b57fabb80d44a00 to your computer and use it in GitHub Desktop.
Like time, but more accurate
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifndef __unix__
#error only supports unix!
#endif // __unix__
#include <unistd.h>
double ExecuteAC(char** argv){
pid_t pid=fork();
if(pid==0){
// child process
execv(argv[0], argv);
exit(127);
}else{
// parent process
long before=clock();
waitpid(pid, 0, 0);
long after=clock();
return (double) (after-before)/CLOCKS_PER_SEC;
}
}
int main(int argc, char** argv){
if(argc<2){
printf("Usage: %s <filenm>", argv[0]);
return 0;
}
printf("Time: %lf", ExecuteAC(argv+1));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment