程序运行时间(windows)

C实现

#include <iostream>  
#include <windows.h>  
using namespace std;
int main(int argc, char **argv)
{
  int year, month, day, hour, minutes, seconds, milliseconds;
  SYSTEMTIME start, end;
  STARTUPINFO si;
    //一些必备参数设置  
      memset(&si, 0, sizeof(STARTUPINFO));
  si.cb = sizeof(STARTUPINFO);
  si.dwFlags = STARTF_USESHOWWINDOW;
  si.wShowWindow = SW_SHOW;
  PROCESS_INFORMATION pi;
    //必备参数设置结束  
      if (!CreateProcess(NULL, argv[1], NULL, NULL, FALSE, 0, NULL, NULL, & si, & pi))
 
  {
      cout << "Create Fail!" << endl;
      exit(1);
     
  }
  else  
  {
      GetSystemTime(&start);
      cout << "Success!" << endl;
     
  }
  WaitForSingleObject(pi.hProcess, INFINITE);
  GetSystemTime(&end);
  milliseconds = end.wMilliseconds - start.wMilliseconds;
  seconds = end.wSecond - start.wSecond;
  minutes = end.wMinute - start.wMinute;
  hour = end.wHour - start.wHour;
  day = end.wDay - start.wDay;
  month = end.wMonth - start.wMonth;
  year = end.wYear - start.wYear;
  if (milliseconds < 0)
     
    {
        seconds--;
        milliseconds += 1000;
       
    }
  if (seconds < 0)
     
    {
        minutes--;
        seconds += 60;
       
    }
  if (hour < 0)
     
    {
        day--;
        hour += 24;
       
    }
  if (day < 0)
     
    {
        month--;
        day += 30;
       
    }
  if (month < 0)
     
    {
        year--;
        month += 12;
       
    }
  if (year > 0)
     
    {
        printf("%d年", year);
       
    }
  if (month > 0)
     
    {
        printf("%d月", month);
       
    }
  if (day > 0)
     
    {
        printf("%d天", day);
       
    }
  if (hour > 0)
     
    {
        printf("%d小时", hour);
       
    }
  if (minutes > 0)
     
    {
        printf("%d分钟", minutes);
       
    }
  if (seconds > 0)
     
    {
        printf("%d秒", seconds);
       
    }
  if (milliseconds > 0)
     
    {
        printf("%d微秒", milliseconds);
       
    }
  printf("\n");
  return 0;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转自http://blog.csdn.net/xugangwen/article/details/44811783...
    扎Zn了老Fe阅读 14,399评论 1 142
  • 昨天在阅读(遇见未知的自己)一书当看到:亲爱的,外面没有别人。这句话时忍不住的分享至朋友圈了。随后收到老公:什么意...
    杏花林阅读 832评论 0 1
  • 感恩今天又下起了小雨,天气虽然阴冷,但是想想明天过后就有太阳出来,心里便又多出了很多希望,期待能见到太阳出来暖洋洋...
    武丹yoyo阅读 1,410评论 0 1
  • 二货哥问5岁的儿子:“你将来娶谁做老婆?” 儿子说:“我要娶奶奶,她疼我!” 二货哥骂道:“放屁!你怎么可以娶我妈...
    老罗xt阅读 2,563评论 0 3
  • 人活在世,确实有好多人需要去感恩,也许他们给了你一个鼓励的眼神,一个温暖的拥抱,一个依靠的港湾,我感恩所有,包...
    半是火焰半是海水阅读 7,328评论 0 0