<address id="ousso"></address>
<form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
  1. C語言

    c++利用windows函數實現計時

    時間:2025-05-04 17:39:38 C語言 我要投稿
    • 相關推薦

    c++利用windows函數實現計時范例

      計時怎樣利用代碼實現呢?以下是為大家分享的c++利用windows函數實現計時范例,供大家參考借鑒,歡迎瀏覽!

      復制代碼 代碼如下:

      //Windows系統下可以用 time(),clock(),timeGetTime(),GetTickCount(),QueryPerformanceCounter()來對一段程序代碼進行計時

      #include

      #include

      #include//time_t time() clock_t clock()

      #include//timeGetTime()

      #pragma comment(lib, "Winmm.lib") //timeGetTime()

      //使用方法:將Sleep()函數換成需要測試運行時間的函數即可。

      int main()

      { //用time()來計時,以秒為單位

      time_t timeBegin, timeEnd;

      timeBegin = time(NULL);

      Sleep(1000);

      timeEnd = time(NULL);

      printf("%dn", timeEnd - timeBegin);

      //用clock()來計時,以毫秒為單位

      clock_t clockBegin, clockEnd;

      clockBegin = clock();

      Sleep(800);

      clockEnd = clock();

      printf("%dn", clockEnd - clockBegin);

      //用timeGetTime()來計時,以毫秒為單位

      DWORD dwBegin, dwEnd;

      dwBegin = timeGetTime();

      Sleep(800);

      dwEnd = timeGetTime();

      printf("%dn", dwEnd - dwBegin);

      //用GetTickCount()來計時,以毫秒為單位

      DWORD dwGTCBegin, dwGTCEnd;

      dwGTCBegin = GetTickCount();

      Sleep(800);

      dwGTCEnd = GetTickCount();

      printf("%dn", dwGTCEnd - dwGTCBegin);

      //用QueryPerformanceCounter()來計時,以微秒為單位

      LARGE_INTEGER large_interger;

      double dff;

      __int64 c1, c2;

      QueryPerformanceFrequency(&large_interger);

      dff = large_interger.QuadPart;

      QueryPerformanceCounter(&large_interger);

      c1 = large_interger.QuadPart;

      Sleep(800);

      QueryPerformanceCounter(&large_interger);

      c2 = large_interger.QuadPart;

      printf("本機高精度計時器頻率%lfn", dff);

      printf("第一次計時器值%I64dn第二次計時器值%I64dn計時器差%I64dn", c1, c2, c2 - c1);

      printf("計時%lf毫秒nn", (c2 - c1) * 1000 / dff);

      return 0;

      }

    【c++利用windows函數實現計時】相關文章:

    c和c++中實現函數回調的方法08-30

    C++函數模板09-14

    C++函數考點歸納09-30

    c++函數指針使用示例07-26

    C/C++函數調用的方式07-29

    C++如何調用matlab函數06-29

    C++函數指針學習教程10-01

    C++中內聯函數的應用09-21

    C++調用C函數的方法05-21

    <address id="ousso"></address>
    <form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
    1. 日日做夜狠狠爱欧美黑人