zoukankan      html  css  js  c++  java
  • 关于计算程序运行时间的方法汇总

    1、GetTickCount()函数

    1.1 函数介绍

      该函数是windows里面常用来计算程序运行时间的函数;

    1.2 使用方法:    

      DWORD dwStart = GetTickCount();
      //这里运行你的程序代码
      DWORD dwEnd = GetTickCount();
      则(dwEnd-dwStart)就是你的程序运行时间, 以毫秒为单位。这个函数只精确到55ms,1个tick就是55ms。

    1.3 代码示例

     1 #include <iostream>
     2 #include <windows.h>
     3 using namespace std;
     4 int main(int argc, char* argv[])
     5 {
     6    DWORD start, end;
     7 
     8    start = GetTickCount();
     9    for(int i=0;i<1000;i++)
    10        cout<<"you are a good child!"<<endl;   //your code
    11    end = GetTickCount()-start;
    12    cout<<end<<endl;
    13    return 0;
    14 }

    2、timeGetTime()函数

    2.1 函数介绍
         timeGetTime()和上一中方法中的GetTickCount()函数类似,但是精度更高。

    2.2 使用说明
      DWORD dwStart = timeGetTime();

       //这里运行你的程序代码

      DWORD dwEnd = timeGetTime();

      则(dwEnd-dwStart)就是你的程序运行时间, 以毫秒为单位。 虽然返回的值单位应该是ms,但传说精度只有10ms。

    2.3 代码示例

     1 #include <iostream>
     2 #include <windows.h>
     3 #pragma comment(lib,"winmm.lib")
     4 
     5 using namespace std;
     6 int main(int argc, char* argv[])
     7 {
     8   DWORD start, end;
     9 
    10   start = timeGetTime();
    11   for(int i=0;i<100;i++)
    12      cout<<"you are a good child!"<<endl;
    13   end = timeGetTime()-start;
    14   cout<<end<<endl;
    15   return 0;
    16 }

    3、clock()函数

    3.1 函数介绍
          用clock()函数,得到系统启动以后的毫秒级时间,然后除以CLOCKS_PER_SEC,就可以换成“秒”,标准c函数。
          clock_t clock ( void );

    3.2 使用说明

          #include <time.h>
          clock_t t = clock();
          long sec = t / CLOCKS_PER_SEC;
          他是记录时钟周期的,实现看来不会很精确,需要试验验证。

    3.3 代码示例

     1 #include<iostream>
     2 #include<ctime> //<time.h>
     3 using   namespace   std;
     4 int   main()
     5 {
     6     time_t   begin,end;
     7 
     8     double duration;
     9     begin=clock();
    10     //这里加上你的代码
    11     end=clock();
    12 
    13     duration=double(end-begin)/CLOCKS_PER_SEC;
    14     cout<<"runtime:   "<<duration<<endl;
    15 }

    5、Ctime类

      CTime MFC类,好像就是把time.h封了个类,没扩展
      CTime t = GetCurrentTime();

    或者

      CTime curTime(t1);
      WORD ms = t1.wMilliseconds;

    6、GetLocalTime()函数 GetSystemTime()函数

    6.1、函数介绍

         WindowsAPI,需要定义SYTEMTIME变量

         SYSTEMTIME 结构包含毫秒信息
         typedef struct _SYSTEMTIME {
               WORD wYear;
               WORD wMonth;
               WORD wDayOfWeek;
               WORD wDay;
               WORD wHour;
           WORD wMinute;
           WORD wSecond;
           WORD wMilliseconds;
        } SYSTEMTIME, *PSYSTEMTIME;

    6.2、使用说明

      SYSTEMTIME t1;
      GetSystemTime(&t1)

    或者

         SYSTEMTIME sysTm;
         ::GetLocalTime(&sysTm);

    7. _strtime()函数

    7.1、函数介绍
      在time.h中的_strtime() //只能在windows中用
    7.2 使用说明

       char t[11];
      _strtime(t);
      puts(t);

  • 相关阅读:
    Codeforces 834D The Bakery
    hdu 1394 Minimum Inversion Number
    Codeforces 837E Vasya's Function
    Codeforces 837D Round Subset
    Codeforces 825E Minimal Labels
    Codeforces 437D The Child and Zoo
    Codeforces 822D My pretty girl Noora
    Codeforces 799D Field expansion
    Codeforces 438D The Child and Sequence
    Codeforces Round #427 (Div. 2) Problem D Palindromic characteristics (Codeforces 835D)
  • 原文地址:https://www.cnblogs.com/jiabei521/p/3113918.html
Copyright © 2011-2022 走看看