windows:
#include <windows.h>
#include <iostream>
using namespace std;
void main(){
LARGE_INTEGER m_liPerfFreq={0};
QueryPerformanceFrequency(&m_liPerfFreq);
LARGE_INTEGER m_liPerfStart={0};
QueryPerformanceCounter(&m_liPerfStart);
for(int i=0; i< 100; i++)
cout << i << endl;
LARGE_INTEGER liPerfNow={0};
QueryPerformanceCounter(&liPerfNow);
int time=( ((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000)/m_liPerfFreq.QuadPart);
cout<<"執行時間:"<<time<<" ms"<<endl;
}
linux:
timeval tim;
gettimeofday(&tim, NULL);
double t1=tim.tv_sec+(tim.tv_usec/1000000.0);
do_something_long();
gettimeofday(&tim, NULL);
double t2=tim.tv_sec+(tim.tv_usec/1000000.0);
printf("%.6lf seconds elapsed\n", t2-t1);
沒有留言:
張貼留言