一。 调用API函数GetTickCount()
#include <windows.h>
#include <iostream>
int main()
{
using namespace std;
DWORD dwStartTick = GetTickCount();
for (int i=0;i<100;i++)
{
cout << i ;
}
DWORD dwDuration = GetTickCount() - dwStartTick;
cout << dwDuration; //返回毫秒
return 0;
}
二。调用库函数clock()
#include <time.h>
#include <iostream>
int main()
{
using namespace std;
double time;
clock_t tStart = clock();
//do your work..
for (int i=0;i<100;i++)
{
cout << i ;
}
time = (double)(clock() - tStart) / CLOCKS_PER_SEC;
cout << time;
return 0;
}