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. mariadb master and salve configure

    mariadb master and salve configure 主从复制配置: master:192.168.8.200 salve:192.168.8.201 主服务器配置: 主服务器需要启动 ...

  2. python笔记--2018-2019

    一:读取json文件的方法 import json json.loads(open('./users.dev.json', 'r').read())     #获取文件的类容,并且序列化把看似列表的字 ...

  3. python3调用阿里云语音服务

    步骤 1 创建阿里云账号,包括语音服务里的企业实名 为了访问语音服务,您需要有一个阿里云账号.如果没有,可首先按照如下步骤创建阿里云账号: 访问阿里云 官方网站,单击页面上的 免费注册 按钮. 按照屏 ...

  4. CentOS 6 & 7 忘记root密码的修改方法

    https://www.linuxidc.com/Linux/2018-01/150211.htm

  5. CentOS下MySQL的彻底卸载

      #################CentOS7下MySQL的卸载#################### 1:查看MySQL是否安装: 方式1: [root@localhost usr]# yu ...

  6. win10开启开发人员模式

    工具: win10 方法如下: 1.在Windows10系统桌面,点击开始菜单,然后在弹出窗口中选择“设置”菜单项 2.在打开的设置窗口中,选择“更新和安全”图标,并点击打开更新和安全窗口 3.在打开 ...

  7. 102. Binary Tree Level Order Traversal + 103. Binary Tree Zigzag Level Order Traversal + 107. Binary Tree Level Order Traversal II + 637. Average of Levels in Binary Tree

    ▶ 有关将一棵二叉树转化为二位表的题目,一模一样的套路出了四道题 ▶ 第 102 题,简单的转化,[ 3, 9, 20, null, null, 15, 7 ] 转为 [ [ 15, 7 ] , [ ...

  8. AES 加密算法 跨语言

    aes加密算法 delphi .java.c# .网页在线工具 4个相同 AES/ECB/PKCS5Padding 与网页在线工具加密结果相同 http://tool.chacuo.net/crypt ...

  9. zookeeper 分布式计数器

    分布式计数器的思路是:指定一个Zookeeper数据节点作为计数器,多个应用实例在分布式锁的控制下,通过更新该数据节点的内容来实现计数功能. Curator中封装了实现,例如 DistributedA ...

  10. Java 目标

    Java 技术 其次掌握的技能树主要有三个方面:第一个是基础,比如对集合类,并发包,IO/NIO,JVM,内存模型,泛型,异常,反射,等有深入了解,最好是看过源码了解底层的设计.比如一般面试都会问Co ...