下面这个函数可以得到微秒级别:

#include<time.h>
int clock_gettime(clockid_t clk_id,struct timespec *tp);
函数"clock_gettime"是基于Linux C语言的时间函数,他可以用于计算精度和纳秒
具体的可以参考 clock_gettime 函数
 
 1 #include <iostream>  
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <sys/time.h>
 5 #include <time.h>
 6 #include <string>
 7 
 8 using namespace std;
 9 
10 string encodeGeohash(const double & theLat, const double & theLong, int thePrecision);
11 
12 int main(int argc, char **argv)
13 {
14     const int MILLION = 1000000;
15     double aLat = -80.11111111111;
16     double aLong = -170.111111111111;
17     struct timespec tpstart;
18     struct timespec tpend;
19     long timedif;
20     clock_gettime(CLOCK_MONOTONIC, &tpstart);
21     int i = 0;
22     for(i = 0; i < 480000000; i++)
23     {
24         encodeGeohash(aLat, aLong, 40);
25         aLat += 0.000001;
26         aLong += 0.000001;
27     }
28 
29     clock_gettime(CLOCK_MONOTONIC, &tpend);
30     timedif = MILLION*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000;
31     std::cout << "i = " << i << "\t timedif = " << timedif << std::endl;
32 
33     return 0;
34 }

 

posted on 2017-02-15 17:07  情月  阅读(584)  评论(0)    收藏  举报