linux编程中,如果用到计时,可以用struct timeval获取系统时间。struct timeval的函数原型如下:

struct timeval {
__kernel_time_t tv_sec; /* seconds */
__kernel_suseconds_t tv_usec; /* microseconds */
};

比如,如果要计算某代码运行的时间,可以使用如下代码:



int main() {

struct timeval tv;
long long start_time, stop_time, delta_time; gettimeofday(&tv, NULL);
start_time= tv.tv_usec; // your code here gettimeofday(&tv, NULL);
stop_time = tv.tv_usec; delta_time = (stop_time - start_time + 1000000)%1000000; }

delta_time就是运行你的代码运行的时间,单位为毫秒。

值得注意的是,tv_usec的最大值是1000000,即1秒,记到1000000后,又会从0开始,所以这里加上1000000再对1000000取余,解决delta_time为负数的问题。

struct timeval 计时问题的更多相关文章

  1. struct timespec 和 struct timeval

    time()提供了秒级的精确度 . 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在 ...

  2. gettimeofday(struct timeval *tv, struct timezone *tz)函数

    gettimeofday(struct timeval *tv, struct timezone *tz)函数 功能:获取当前精确时间(Unix时间) 其中: timeval为时间 truct tim ...

  3. linux高精度struct timespec 和 struct timeval

    一.struct timespec 定义: typedef long time_t;#ifndef _TIMESPEC#define _TIMESPECstruct timespec {time_t ...

  4. struct timeval结构体 以及 gettimeofday()函数(转)

    struct timeval结构体 转载地址:http://blog.chinaunix.net/uid-20548989-id-2533161.html 该结构体是Linux系统中定义,struct ...

  5. struct timeval和gettimeofday()

    http://www.cppblog.com/lynch/archive/2011/08/05/152520.html struct timeval结构体在time.h中的定义为: struct ti ...

  6. struct timeval 和 struct timespec

    struct timeval { time_t tv_sec; suseconds_t tv_usec; }; 測试代码例如以下: #include <stdio.h> #include ...

  7. struct timeval和gettimeofday

    struct timeval和gettimeofday() struct timeval结构体在time.h中的定义为: struct timeval { time_t tv_sec; /* Seco ...

  8. [c++]struct timeval

    struct timeval { time_t tv_sec; // seconds long tv_usec; // microseconds }; re 1. struct timespec 和 ...

  9. 003.同时Ping多个IP(select实现IO复用,信号计时),ping程序升级版

    写这个的目的主要是为了以后的方便: 1.信号计时函数的使用 2.ip头的构建和icmp头的构建 3.selec函数t的用法 代码实现: /src/ping.h /* * ping.h * * Crea ...

随机推荐

  1. ipython的使用

    改初始路径 还有一个坑,可以用notebook打开一个已经存在的文件,但是不能正常编辑(使用单元编辑),因为使用这个创建的东西根本就不是一个.py文件,如果代码编辑完毕,倒是可以通过下载那里选择下载成 ...

  2. 10-28质量监控ELK

    监控业务范围 app崩溃监控(Bugly) 应用性能监控(APM) 业务监控(TalkingData.友盟) 质量监控(缺位) 质量监控平台ELK elk官网 数据构造 线上错误状态分布 故障影响范围 ...

  3. CFGym 100211J 题解

    一.题目 二.题意 给定一个字母表(最多也就是英文小写字母的前10个字母),一个交换表,两个字符串,判断字符串A能否通过交换表的交换方式变成字符串B. 三.思路 1.一开始,比赛时,我半模拟半记忆化地 ...

  4. 常见报表的JS代码

    1.合并单元格 这个表格在报表里面算是比较典型的 1.里面的表格的列标题现在是写死的,其实这些可以通过配置进行 2.至于如果表头要进行合并(这种需求比较少,也比较容易实现) 3.至于统计:最好在后台按 ...

  5. CentOS 6.7 编译PHP7 make时出现错误:undefined reference to `libiconv_close’

    编辑Makefile文件,找到变量EXTRA_LIBS,并在末尾添上-liconv EXTRA_LIBS = -lcrypt -lz -lexslt -lcrypt -lrt -lmcrypt -ll ...

  6. 「小程序JAVA实战」小程序视频展示页开发(52)

    转自:https://idig8.com/2018/09/22/xiaochengxujavashizhanxiaochengxushipinzhanshiyekaifa51/ 这次说下,小程序的视频 ...

  7. js练习 原型

    //var a = {        //    fun: function a() {        //        test = 0;        //        alert(this) ...

  8. Liunx下如何使用kettle

    在windows下完成所有操作, 把xxx.ktr上传到liunx, Pan.sh xxx.ktr 就完成了

  9. jQuery的过滤器总结

    1.内容过滤器 $(function () { // $("a:contains('标签')").css("color","green") ...

  10. 20-调用百度AI的文字识别

    本来准备自己写识别的,貌似现在能力不足,直接偷懒用百度的api吧 from aip import AipOcr """ 你的 APPID AK SK "&quo ...