跨平臺的計時函數 支持win/linux

源文件

#if defined(WIN32) || defined(_WIN32)   // Windows system specific
#include <windows.h>
#else          // Unix based system specific
#include <sys/time.h>
#endif

__int64 getCount()
{
#if defined(WIN32) || defined(_WIN32)
    LARGE_INTEGER _count;
    QueryPerformanceCounter(&_count);
    return _count.QuadPart;
#else
    timeval _count;
    gettimeofday(&_count, NULL);
    return (_count.tv_sec * 1000000.0) + _count.tv_usec;
#endif
}

double duration_ms(__int64 start, __int64 end)
{
#if defined(WIN32) || defined(_WIN32)
    LARGE_INTEGER frequency;
    QueryPerformanceFrequency(&frequency);
    return (end - start) * (1000.0 / frequency.QuadPart);
#else
    return (end - start) / 1000.0;
#endif
}

double duration_from(__int64 start)
{
#if defined(WIN32) || defined(_WIN32)
    LARGE_INTEGER _count;
    QueryPerformanceCounter(&_count);
    __int64 end = _count.QuadPart;
    LARGE_INTEGER frequency;
    QueryPerformanceFrequency(&frequency);
    return (end - start) * (1000.0 / frequency.QuadPart);
#else
    timeval _end;
    gettimeofday(&_end, NULL);
    return ((_end.tv_sec * 1000000.0) + _end.tv_usec - start) / 1000.0;
#endif
}

頭文件

#ifndef TIMESTAMP_H
#define TIMESTAMP_H

__int64 getCount();
double duration_ms(__int64 start, __int64 end);
double duration_from(__int64 start);

#endif //TIMESTAMP_H

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章