zoukankan      html  css  js  c++  java
  • Linux c++ time different

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

    #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 << "	 timedif = " << timedif << std::endl;
    32 
    33     return 0;
    34 }
  • 相关阅读:
    js模板引擎
    浮点数正则表达式
    DbContext SQLite配置文件
    JS中的HTML片段
    WPF 使用HttpListener搭建本地web服务器
    C#调用Windows(8/10)自带的虚拟键盘
    SQLSERVER 设置默认值
    SQLSERVER存储过程基本语法
    MSSQL存储过程
    WPF手动绑定事件
  • 原文地址:https://www.cnblogs.com/AndyStudy/p/6402240.html
Copyright © 2011-2022 走看看