struct timeval 和 struct timespec
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相关推荐
- 结构体struct timeval 和 struct timespec的定义
结构体struct timeval 和 struct timespec的定义均在头文件<sys/time.h>中,具体定义如下: struct timeval {long tv_sec; ...
- struct timeval 和 struct timespec 应用小结
在基于linux的C编程中,经常会看到 struct timeval和struct timespec 这两个跟时间有关的结构体,有时候会容易混淆,先看下这两个结构体的定义,以linux-2.6.35为 ...
- Linux时间相关知识小结:struct timeval、timespec、gettimeofday、time、localtime....
前言 我们在linux平台进行开发时,时间相关的操作基本上都会遇到,本文就对常用的时间相关的结构体.接口进行分析小结. 常见类型.结构体定义 timespec 原型 struct timespec { ...
- Linux高精度struct timespec和struct timeval
1. 结构基本定义 1.1 struct timespec 定义 typedef long time_t; #ifndef _TIMESPEC #define _TIMESPEC struct tim ...
- Linux高精度struct timespec 和 struct timeval
struct timespec now_ts;clock_gettime(CLOCK_MONOTONIC, &now_ts);printf("os tick times, %ld s ...
- struct timespec 和 struct timeval
struct timespec 和 struct timeval time()提供了秒级的精确度 . 1.头文件 <time.h> 2.函数原型 time_t time(time_t * ...
- struct timeval用法与时间溢出问题
1.用法 高精度计算时间的结构体struct timeval. 头文件是sys/time.h 网上查找的定义是 #include "sys/time.h"struct timeva ...
- linux系统中struct timeval结构体、struct timezone结构体以及gettimeofday函数
格林尼治时间.协调世界时 间.世界时间.日光节约时间以及时区等介绍: 格林尼治时间(Greenwich Mean Time,GMT)是指位于英国伦敦郊区的皇家格林尼治天文台当地的标准时间,因为本初子午 ...
- struct timeval的处理(timeval比较及相减)
timeval结构体的处理函数 timeval的大小比较 timeval转为double类型的秒 两个timeval类型之间的时间差 函数耗时测试 timeval的大小比较 /************ ...
最新文章
- 【Python3_进阶系列_006】Python3-单例模式
- 嵌入式第一周学习总结
- spring boot第二讲
- Windows系统调用学习笔记(三)—— 保存现场
- 新款苹果电脑_苹果发布新款MacBookAir 搭载M1芯片7999起
- pip配置永久国内源
- 四路由器的OSPF DR ,BDR
- js之数据类型及类型转换
- Python刷csdn访问量
- YOLOv3通道+层剪枝,参数压缩98%,砍掉48个层,提速2倍!
- rk3399_android7.1添加个驱动且加上宏控编译
- C++使用opencv判断一个点是否在多边形之内
- SQL中笛卡尔积-cross join的用法
- Unity小游戏教程系列 | 创建小型太空射击游戏(一)
- Android--刷机教程
- Hystrix学习——(2)雪崩效应
- 学并发编程,透彻理解这三个核心是关键
- 工业控制靶场记录以及工业控制协议的简单介绍
- 运动目标检测跟踪主流算法
- Lenovo 拯救者15ISK升级BIOS
热门文章
- selenium之用chrome的Mobile emulation模拟手机浏览器测试手机网页
- ATM Capital 完成首轮募资瞄准东南亚,阿里eWTP科技创新基金为基石投资人...
- 教你怎么用电脑录制麦克风里面的声音
- 【AI名利场·公司】也喊All in AI,看出门问问的新战略
- DOS攻击(denial-of-service)与防范
- Linux信号signal介绍,sigaction结构体,signal()函数,sigaction()函数
- python画动漫图片_python下载动漫图片
- 自动化办公-PDF篇
- LTE中小区选择流程及其S准则详解
- php7.1 phpize,PHP扩展安装之phpize