[C/C++] Timer

  • Timer
  • 最近在做一些效能的測試需要用到比較精準的Timer,分別查了一下給Windows和Linux平台的寫法。

    For Linux:

    在Linux裡,則是使用timeval來計算時間,理論上精準到micro second

    #include <stdio.h>
    #include <sys/time.h>

    int main()
    {
    int i;
    struct timeval t1, t2;
    double elapsedTime;

    gettimeofday(&t1, NULL);
    gettimeofday(&t2, NULL);

    elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
    elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
    printf("%.3f ms\n",elapsedTime);
    return 0;
    }

    For Windows:

    Windows部份使用QueryPerformanceFrequency()這個指令,理論上是精準到跟時脈值一樣

    #include <stdio.h>
    #include <windows.h>
    int main()
    {
    LARGE_INTEGER frequency;
    LARGE_INTEGER t1, t2;
    double elapsedTime;

    QueryPerformanceFrequency(&frequency);

    QueryPerformanceCounter(&t1);
    QueryPerformanceCounter(&t2);

    elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart;
    printf("%.3f ms\n", elapsedTime);
    return 0;
    }

No comments:

Post a Comment

Orange - data analysis tool

Installation pip install orange3 Run orange python -m Orange.canvas