求某段程序运行的高精度时间
Linux环境下:
/**
struct timeval
{
time_t tv_sec; // 秒
suseconds_t tv_usec; // 微妙 (10的负六次方)
};
typedef struct timespec{
time_t tv_sec;//秒
long tv_nsex;//纳秒
}timespec_t;
**/
#include <algorithm>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
double getTime()
{
struct timeval tv;
gettimeofday(&tv, NULL);
//return tv.tv_sec + tv.tv_usec / 1000000.0;
return tv.tv_sec + tv.tv_usec;
}
using namespace std;
int main()
{
vector<int> vec(1024*1024/sizeof(int));
for (int i = 0; i < vec.size(); ++i)
vec[i] = lrand48();
printf("sorting %zd elements\n", vec.size());
double sorting = getTime();
sort(vec.begin(), vec.end());
double done = getTime();
printf("sort %f micros\n", done - sorting);
return 0;
}
在windows下统计程序运行高精度时间参见另外一篇文章 “1G 数据在入门双核中运行”。