依然计时

   条件编译,vc 和 gcc都能用的计时函数.

#ifdef _MSC_VER  // If we're compiling for Windows

#include <windows.h>

typedef LARGE_INTEGER TimerType;
inline TimerType getTimer()
{
    LARGE_INTEGER t;
    QueryPerformanceCounter(&t);
    return t;
}

inline double interval(TimerType start, TimerType end)
{
    LARGE_INTEGER ticksPerSecond;
    QueryPerformanceFrequency(&ticksPerSecond);
    return (1000.0 * (end.QuadPart - start.QuadPart)) / ticksPerSecond.QuadPart;
}

#else // If we're not compiling for Windows, use Standard C

#include <ctime>

typedef clock_t TimerType;
inline TimerType getTimer() { return clock(); }
inline double interval(TimerType start, TimerType end)
{
    return (1000.0 * (end - start)) / CLOCKS_PER_SEC;
}

#endif  // ifdef _MSC_VER

 windows下有这个: LARGE_INTEGER 【以后可试试】。

 使用:

 startSort = getTimer();
 sort(stores.begin(), stores.end(), compareStore);
 endSort = getTimer();
 report("STL sort", interval(startSort, endSort), stores);

   

你可能感兴趣的:(计时)