美文网首页
程序运行时间(windows)

程序运行时间(windows)

作者: SummerC0ld | 来源:发表于2017-04-06 15:25 被阅读0次

    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;
    }
    
    

    相关文章

      网友评论

          本文标题:程序运行时间(windows)

          本文链接:https://www.haomeiwen.com/subject/aorqattx.html