C语言 程序时间统计器
Unix command time for windows.
C++代码
- // time.cpp : Defines the entry point for the console application.
- //
-
#include
</span> </span> </li> - #include
</span> </span> </li> - #include
</span> </span> </li> - int gettime() {
- time_t t;
- time(&t);
- return t;
- }
- void printtime(int t) {
- char buf[20];
- if (t<0) sprintf(buf,"%d s",0);
- else if (t<60) sprintf(buf,"%d s",t);
- else if (t<3600) sprintf(buf,"%d m %d s",t/60, t%60);
- else sprintf(buf,"%d h %d m %d s",t/3600, (t%3600)/60, (t%3600)%60);
- puts(buf);
- }
- int main(int argc, char* argv[])
- {
- if(argc<2) {
- puts("Nothing to run.");
- return 0;
- }
- STARTUPINFO si={0};
- PROCESS_INFORMATION pi;
- int i,t1,t2;
- char argstr[MAX_PATH]={""};
- si.cb=sizeof(STARTUPINFO);
- t1=gettime();
- for(i=1;argv[i];i++) {
- strcat(argstr,argv[i]);
- strcat(argstr," ");
- }
- //puts(argv[1]);
- //printf("%d\n",t1);
- if(CreateProcess(NULL,argstr,NULL,NULL,FALSE,0,NULL,NULL,&si,π)) {
- WaitForSingleObject(pi.hProcess,INFINITE);
- t2=gettime();
- //printf("%d\n",t2);
- printtime(t2-t1);
- } else {
- printf("Unable to run app!");
- }
- return 0;
- }
</ol> </div> 仿linux的time程序。 - #include