struct timeval 计时问题
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 计时问题的更多相关文章
- struct timespec 和 struct timeval
time()提供了秒级的精确度 . 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在 ...
- gettimeofday(struct timeval *tv, struct timezone *tz)函数
gettimeofday(struct timeval *tv, struct timezone *tz)函数 功能:获取当前精确时间(Unix时间) 其中: timeval为时间 truct tim ...
- linux高精度struct timespec 和 struct timeval
一.struct timespec 定义: typedef long time_t;#ifndef _TIMESPEC#define _TIMESPECstruct timespec {time_t ...
- struct timeval结构体 以及 gettimeofday()函数(转)
struct timeval结构体 转载地址:http://blog.chinaunix.net/uid-20548989-id-2533161.html 该结构体是Linux系统中定义,struct ...
- struct timeval和gettimeofday()
http://www.cppblog.com/lynch/archive/2011/08/05/152520.html struct timeval结构体在time.h中的定义为: struct ti ...
- struct timeval 和 struct timespec
struct timeval { time_t tv_sec; suseconds_t tv_usec; }; 測试代码例如以下: #include <stdio.h> #include ...
- struct timeval和gettimeofday
struct timeval和gettimeofday() struct timeval结构体在time.h中的定义为: struct timeval { time_t tv_sec; /* Seco ...
- [c++]struct timeval
struct timeval { time_t tv_sec; // seconds long tv_usec; // microseconds }; re 1. struct timespec 和 ...
- 003.同时Ping多个IP(select实现IO复用,信号计时),ping程序升级版
写这个的目的主要是为了以后的方便: 1.信号计时函数的使用 2.ip头的构建和icmp头的构建 3.selec函数t的用法 代码实现: /src/ping.h /* * ping.h * * Crea ...
随机推荐
- Java 利用缓冲字节流来实现视频、音频、图片的复制粘贴
InputStream:继承自InputStream的流都是用于向程序中输入数据的,且数据单位都是字节(8位). OutputSteam:继承自OutputStream的流都是程序用于向外输出数据的, ...
- [原创][C#.Winform 控件]Krypton Suite comments
命名空间:ComponentFactory.Krypton.Toolkit 最新版本:v4.4.0 官网状态:已停用 下载地址1:http://downloads.informer.com/krypt ...
- postman-3http请求
http消息是服务器和客户端之间交换数据的方式. 有2种类型的消息: 请求:由客户端发送用来触发服务器的动作. 响应:来自服务器的应答. https://developer.mozilla.org/z ...
- 同一客户端使用多份SSH Key
创建或添加如下内容: Host example1.com HostName realname.example.com IdentityFile ~/.ssh/example1_rsa # 私钥 Hos ...
- SQL中INNER JOIN的用法
SQL join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据. Join 和 Key 有时为了得到完整的结果,我们需要从两个或更多的表中获取结果.我们就需要执行 join. 数据库中的表 ...
- angularjs中的$http详解
语法: 要将区别先弄清$http服务,它是对原生XMLHttpRequest对象的简单封装,是只能接受一个参数的方法, 这个方法会返回一个promise对象,具有sccess和error两个方法.当然 ...
- leetcode507
public class Solution { public bool CheckPerfectNumber(int num) { ) { return false; } ; ; i <= nu ...
- 跟我学算法 - 读取excel文件(xlrd)
import xlrd import numpy as np # fname 表示文件名 fname = '1白.xlsx'# 打开文件 bk = xlrd.open_workbook(fname)# ...
- eclipse报Access restriction: The type 'BASE64Decoder' is not API处理方法
今天从svn更新代码之后,由于代码中使用了BASE64Encoder 更新之后报如下错误: Access restriction: The type ‘BASE64Decoder’ is not A ...
- pg_hba.conf、pool_hba.conf 以及 pool_passwd 三者间的关系
pg_hba.conf.pool_hba.conf 以及 pool_passwd 三者间的关系: 1.pg_hba.conf.pool_hba.conf 以及 pool_passwd 三者关系 pg_ ...