Skip to content

Instantly share code, notes, and snippets.

@aldente39
Created January 29, 2012 09:51
Show Gist options
  • Save aldente39/1698077 to your computer and use it in GitHub Desktop.
Save aldente39/1698077 to your computer and use it in GitHub Desktop.
timeit command
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<windows.h>
int main(int argc, char *argv[]){
clock_t start, end;
FILETIME creationTime, exitTime;
FILETIME kernelTime, userTime;
SYSTEMTIME ek, eu;
PROCESS_INFORMATION pi;
STARTUPINFO si;
int i;
int flag = 1;
char arg[255] = "";
if(argc < 2){
printf("Usage: timeit \"program name\"\n");
return 1;
}
else{
for(i = 1; i < argc; i++){
strcat(arg, argv[i]);
strcat(arg, " ");
}
}
ZeroMemory(&si,sizeof(si));
si.cb=sizeof(si);
printf("--------------------------------------------------\n\n");
start = clock();
//実行ファイルのみできる。内部コマンド、ファイル名単体は駄目ぽい。
flag = CreateProcess(NULL,(LPSTR)arg,NULL,NULL,FALSE,
NORMAL_PRIORITY_CLASS,
NULL,NULL,&si,&pi);
WaitForSingleObject(pi.hProcess, INFINITE);
end = clock();
GetProcessTimes(pi.hProcess,&creationTime,&exitTime,
&kernelTime,&userTime);
printf("\n--------------------------------------------------\n");
if(flag == 0){
printf("Usage: timeit \"program name\"\n");
return 1;
}
long long int* uTime = (long long int*)&userTime;
long long int* kTime = (long long int*)&kernelTime;
double uSec = *uTime / 10000000.0;
double kSec = *kTime / 10000000.0;
printf("real: %f sec.\n", ((float)(end-start))/1000.0);
printf("user: %f sec.\n",uSec);
printf("kernel: %f sec.\n",kSec);
printf("flag: %d\n", flag);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment