1. 使用timeval,微秒级(1s = 1 000ms = 1 000 000us = 1 000 000 000ns)

struct timeval
{long tv_sec;  //secondslong tv_usec; //microseconds
}

具体应用:

#include<time.h>
#include<sys/time.h>struct timeval T0,T1;
double ellapsedT = 0;gettimeofday(&T0, NULL);  XX_gen(key);gettimeofday(&T1, NULL); ellapsedT += (T1.tv_sec-T0.tv_sec) + (double)(T1.tv_usec-T0.tv_usec) / (1e+6);printf("\nTime for XX_gen is %fs\n", ellapsedT);

2. 使用timespec,纳秒级

typedef long time_t;
#ifndef _TIMESPEC
#define _TIMESPECstruct timespec
{time_t tv_sec; // secondslong tv_nsec; // nanoseconds
};#endif

具体使用:

struct timespec start, end; //nanoseconds
double time_gen;
time_gen = 0.0;clock_gettime(CLOCK_MONOTONIC, &start);XX_gen(key);clock_gettime(CLOCK_MONOTONIC, &end);time_gen += (double) (1000 * (end.tv_sec - start.tv_sec)) + (double) (end.tv_nsec - start.tv_nsec)/1000000;printf("\nTime for XX_gen is %8.6f mSec. \n",  time_gen);

3. 获得CPU周期数

uint64_t rdtsc()
{uint32_t hi, lo;__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));return ( (uint64_t)lo)|( ((uint64_t)hi)<<32 );
}
/* timing */
#ifdef WIN#define current_stamp(a)  __asm__ __volatile__("rdtsc":"=a"(((unsigned int *)(a))[0]),"=d"(((unsigned int *)a)[1]))
#else#define current_stamp(a)  __asm__ __volatile__("rdtsc":"=a"(((unsigned int *)(a))[0]),"=d"(((unsigned int *)a)[1]))
#endiftypedef unsigned long long DWORD; /* Should be 64-bit = 8 bytes */ DWORD start_cycle, end_cycle, CPU_cycles = 0;
double ellapsedT = 0;double CPU_GHz, T_cycles_per_byte;current_stamp(&start_cycle);
gettimeofday(&T0, NULL);  XX_gen(key);gettimeofday(&T1, NULL);
current_stamp(&end_cycle); CPU_cycles += end_cycle-start_cycle; ellapsedT += (T1.tv_sec-T0.tv_sec) + (double)(T1.tv_usec-T0.tv_usec) / (1e+6);CPU_GHz = CPU_cycles/(1e+9)/ellapsedT;  T_cycles_per_byte = ((double)CPU_cycles/0xC7); printf("\nDetected effective CPU speed is %lf GHz.\n", CPU_GHz);printf("\nSpeed is %.2lf cycles per byte for XX_gen.\n", T_cycles_per_byte);printf("\nTime for XX_gen is %fs\n", ellapsedT);

struct timeval 和 struct timespec相关推荐

  1. 结构体struct timeval 和 struct timespec的定义

    结构体struct timeval 和 struct timespec的定义均在头文件<sys/time.h>中,具体定义如下: struct timeval {long tv_sec; ...

  2. struct timeval 和 struct timespec 应用小结

    在基于linux的C编程中,经常会看到 struct timeval和struct timespec 这两个跟时间有关的结构体,有时候会容易混淆,先看下这两个结构体的定义,以linux-2.6.35为 ...

  3. Linux时间相关知识小结:struct timeval、timespec、gettimeofday、time、localtime....

    前言 我们在linux平台进行开发时,时间相关的操作基本上都会遇到,本文就对常用的时间相关的结构体.接口进行分析小结. 常见类型.结构体定义 timespec 原型 struct timespec { ...

  4. Linux高精度struct timespec和struct timeval

    1. 结构基本定义 1.1 struct timespec 定义 typedef long time_t; #ifndef _TIMESPEC #define _TIMESPEC struct tim ...

  5. Linux高精度struct timespec 和 struct timeval

    struct timespec now_ts;clock_gettime(CLOCK_MONOTONIC, &now_ts);printf("os tick times, %ld s ...

  6. struct timespec 和 struct timeval

    struct timespec 和 struct timeval time()提供了秒级的精确度 . 1.头文件 <time.h> 2.函数原型 time_t time(time_t * ...

  7. struct timeval用法与时间溢出问题

    1.用法 高精度计算时间的结构体struct timeval. 头文件是sys/time.h 网上查找的定义是 #include "sys/time.h"struct timeva ...

  8. linux系统中struct timeval结构体、struct timezone结构体以及gettimeofday函数

    格林尼治时间.协调世界时 间.世界时间.日光节约时间以及时区等介绍: 格林尼治时间(Greenwich Mean Time,GMT)是指位于英国伦敦郊区的皇家格林尼治天文台当地的标准时间,因为本初子午 ...

  9. struct timeval的处理(timeval比较及相减)

    timeval结构体的处理函数 timeval的大小比较 timeval转为double类型的秒 两个timeval类型之间的时间差 函数耗时测试 timeval的大小比较 /************ ...

最新文章

  1. 【Python3_进阶系列_006】Python3-单例模式
  2. 嵌入式第一周学习总结
  3. spring boot第二讲
  4. Windows系统调用学习笔记(三)—— 保存现场
  5. 新款苹果电脑_苹果发布新款MacBookAir 搭载M1芯片7999起
  6. pip配置永久国内源
  7. 四路由器的OSPF DR ,BDR
  8. js之数据类型及类型转换
  9. Python刷csdn访问量
  10. YOLOv3通道+层剪枝,参数压缩98%,砍掉48个层,提速2倍!
  11. rk3399_android7.1添加个驱动且加上宏控编译
  12. C++使用opencv判断一个点是否在多边形之内
  13. SQL中笛卡尔积-cross join的用法
  14. Unity小游戏教程系列 | 创建小型太空射击游戏(一)
  15. Android--刷机教程
  16. Hystrix学习——(2)雪崩效应
  17. 学并发编程,透彻理解这三个核心是关键
  18. 工业控制靶场记录以及工业控制协议的简单介绍
  19. 运动目标检测跟踪主流算法
  20. Lenovo 拯救者15ISK升级BIOS

热门文章

  1. selenium之用chrome的Mobile emulation模拟手机浏览器测试手机网页
  2. ATM Capital 完成首轮募资瞄准东南亚,阿里eWTP科技创新基金为基石投资人...
  3. 教你怎么用电脑录制麦克风里面的声音
  4. 【AI名利场·公司】也喊All in AI,看出门问问的新战略
  5. DOS攻击(denial-of-service)与防范
  6. Linux信号signal介绍,sigaction结构体,signal()函数,sigaction()函数
  7. python画动漫图片_python下载动漫图片
  8. 自动化办公-PDF篇
  9. LTE中小区选择流程及其S准则详解
  10. php7.1 phpize,PHP扩展安装之phpize