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 ...
随机推荐
- pytest框架 里 fixture 参数化的方法
- Linux下添加php的zip模块
今天早上开发的人员过来跟我说,测试机上的XX项目报了个错: include(ZipArchive.php): failed to open stream: No such file or direct ...
- hadoop学习day3 mapreduce笔记
1.对于要处理的文件集合会根据设定大小将文件分块,每个文件分成多块,不是把所有文件合并再根据大小分块,每个文件的最后一块都可能比设定的大小要小 块大小128m a.txt 120m 1个块 b.txt ...
- OpenCL Hello World
▶ OpenCL 的环境配置与第一个程序 ● CUDA 中自带 OpenCL 需要的头文件和库,直接拉近项目里边去就行:AMD 需要下载 AMD APP SDK(https://community.a ...
- libcurl 调用curl_easy_getinfo( ) 返回错误码对照
//执行设置好的操作 res = curl_easy_perform(easy_handle); //获取HTTP错误码 ; curl_easy_getinfo(easy_handle, CURLIN ...
- tomcat加固
tomcat安全加固和规范 tomcat是一个开源Web服务器,基于Tomcat的Web运行效率高,可以在一般的硬件平台上流畅运行,因此,颇受Web站长的青睐.不过,在默认配置下其存在一定的安全隐患, ...
- python's default parameter
[python's default parameter] 对于值类型(int.double)的default函数参数,函数不会保存对默认类型的修改.对于mutable objectd类型的默认参数,会 ...
- 133. Clone Graph (Graph, Map; DFS)
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 119. Pascal's Triangle II (Graph; WFS)
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 基于注解的方式管理Bean
--------------------siwuxie095 基于注解的方式管理 Bean (一)准备 ...