#include <Windows.h> #include <iostream> using namespace std; typedef void (*RUNFUN)(); DWORD GetFunRunTime(RUNFUN pfun); void Run(); class CStopWatch { public: CStopWatch() { QueryPerformanceFrequency(&m_liPerfFreq); Start(); } ~CStopWatch() { } public: void Start() { QueryPerformanceCounter(&m_liPerfStart); } __int64 Now() const { LARGE_INTEGER liPerfNow = {0}; QueryPerformanceCounter(&liPerfNow); return ((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000) / m_liPerfFreq.QuadPart; } private: LARGE_INTEGER m_liPerfFreq; LARGE_INTEGER m_liPerfStart; }; int main() { CStopWatch objWatch; cout << "Run() use time: " << GetFunRunTime(Run) << endl; cout << "process use time: " << objWatch.Now() << endl; return 0; } DWORD GetFunRunTime(RUNFUN pfun) { DWORD dwStartTime = 0; DWORD dwEndTime = 0; dwStartTime = GetTickCount(); pfun(); dwEndTime = GetTickCount(); return dwEndTime - dwStartTime; } void Run() { int i = 0; for (i = 0; i < 100; i++) { cout << "Hello, andylin!" << endl; } }