linux中C语言获取高精度时钟gettimeofday函数
前言:
在开发中,很多时候需要知道各个函数或者是某些设备对命令的操作用时,因此需要用到 gettimeofday 来获取当前时钟。
一,函数说明
#include
int gettimeofday(struct timeval *tv, struct timezone *tz);
注意:
2.受系统时间修改影响
3.返回的秒数是从1970年1月1日0时0分0秒开始
其参数tv是保存获取时间结果的结构体,参数tz用于保存时区结果:
结构体timeval的定义为:
点击(此处)折叠或打开
struct timeval
{
long int tv_sec; // 秒数
long int tv_usec; // 微秒数
}
它获得的时间精确到微秒(1e-6 s)量级
结构体timezone的定义为:
点击(此处)折叠或打开
struct timezone
{
int tz_minuteswest;/*格林威治时间往西方的时差*/
int tz_dsttime; /*DST 时间的修正方式*/
}
timezone 参数若不使用则传入NULL即可。
其中 tz_dsttime 的值:
点击(此处)折叠或打开
DST_NONE /*不使用*/
DST_USA /*美国*/
DST_AUST /*澳洲*/
DST_WET /*西欧*/
DST_MET /*中欧*/
DST_EET /*东欧*/
DST_CAN /*加拿大*/
DST_GB /*大不列颠*/
DST_RUM /*罗马尼亚*/
DST_TUR /*土耳其*/
DST_AUSTALT /*澳洲(1986年以后)*/
返回值
成功则返回0,失败返回-1,错误代码存于errno。
ERRORS
EFAULT One of tv or tz pointed outside the accessible address space.
EINVAL Timezone (or something else) is invalid.
二,实例
点击(此处)折叠或打开
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
int time_substract(struct timeval *result, struct timeval *begin,struct timeval *end)
{
if(begin->tv_sec > end->tv_sec) return -;
if((begin->tv_sec == end->tv_sec) && (begin->tv_usec > end->tv_usec)) return -;
result->tv_sec = (end->tv_sec - begin->tv_sec);
result->tv_usec = (end->tv_usec - begin->tv_usec); if(result->tv_usec < )
{
result->tv_sec--;
result->tv_usec += ;
}
return ;
}
int main(int argc, char **argv)
{
struct timeval start,stop,diff;
memset(&start,,sizeof(struct timeval));
memset(&stop,,sizeof(struct timeval));
memset(&diff,,sizeof(struct timeval));
gettimeofday(&start,);
//做你要做的事...
printf("hello world\n");
gettimeofday(&stop,);
time_substract(&diff,&start,&stop);
printf("Total time : %d s,%d us\n",(int)diff.tv_sec,(int)diff.tv_usec);
}

linux中C语言获取高精度时钟gettimeofday函数的更多相关文章
- (二十)linux中i2c的ioctl,write,read函数的使用
一.ioctl函数的使用:原型:struct ioctl(struct file *file,unsigned int cmd,unsigned long arg);cmd有I2C_SLAVE,I2C ...
- Linux中与环境变量相关的函数
1.在终端可以通过env.set命令查看当前的环境变量 2.通过main函数中的第三个参数可以得到当前进程的环境变量列表 int main(int argc , char *argv[] , char ...
- linux 中的 open() read() write() close() 函数
1. open()函数 功能描述:用于打开或创建文件,在打开或创建文件时可以指定文件的属性及用户的权限等各种参数. 所需头文件:#include <sys/types.h>,#includ ...
- Linux 多线程应用中如何编写安全的信号处理函数
http://blog.163.com/he_junwei/blog/static/1979376462014021105242552/ http://www.ibm.com/developerwor ...
- linux中应用程序main函数中没有开辟进程的,它应该在那个进程中运行呢?
1.main函数是一个进程还是一个线程? 不知道你是用c创建的,还是用java创建的. 因为它们都是以main()做为入口开始运行的. 是一个线程,同时还是一个进程. 在现在的操作系统中,都是多线程的 ...
- Linux 多线程应用中如何编写安全的信号处理函数【转】
转自:https://www.cnblogs.com/virusolf/p/4945642.html http://blog.163.com/he_junwei/blog/static/1979376 ...
- mips64高精度时钟引起ktime_get时间不准,导致饿狗故障原因分析【转】
转自:http://blog.csdn.net/chenyu105/article/details/7720162 重点关注关中断的情况.临时做了一个版本,在CPU 0上监控所有非0 CPU的时钟中断 ...
- Linux中main是如何执行的
Linux中main是如何执行的 这是一个看似简单的问题,但是要从Linux底层一点点研究问题比较多.找到了一遍研究这个问题的文章,但可能比较老了,还是在x86机器上进行的测试. 原文链接 开始 问题 ...
- Linux中的两个经典宏定义:获取结构体成员地址,根据成员地址获得结构体地址;Linux中双向链表的经典实现。
倘若你查看过Linux Kernel的源码,那么你对 offsetof 和 container_of 这两个宏应该不陌生.这两个宏最初是极客写出的,后来在Linux内核中被推广使用. 1. offse ...
随机推荐
- 数组(Array)的使用方法
本文内容: 1.概述 2.数组基础 3.结合for循环与arr.length,在数组尾部插入数值 4.利用'concat','join'实 ...
- 吉日嘎拉DotNet.BusinessV4.2中的一处bug,及我的修复和扩展
bug所在位置:DotNet.Business\Utilities\BaseManager.GetDataTableByPage.cs的函数 public virtual DataTable GetD ...
- cppcheck
http://sourceforge.net/projects/cppcheck/files/?source=navbar https://github.com/danmar/cppcheck htt ...
- u-boot移植总结(四)u-boot-2010.09框架分析
(一)本次移植是基于FL2440,板子的基本硬件: CPU 型号为S3C2440,基于ARM920T,指令集ARMV4,时钟主频400MHz SDRAM H57V2562GTR-75C 2片*32MB ...
- POJ 1681---Painter's Problem(高斯消元)
POJ 1681---Painter's Problem(高斯消元) Description There is a square wall which is made of n*n small s ...
- 回文串---吉哥系列故事——完美队形II
HDU 4513 Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出 ...
- 配置ssh免密码连接
建立ssh连接步骤: 1,在主机安装ssh-server,执行指令: apt-get install openssh-server 2,在主机上执行指令: netstat -atpn | grep 可 ...
- elasticsearch的mapping映射
Mapping简述 Elasticsearch是一个schema-less的系统,但并不代表no shema,而是会尽量根据JSON源数据的基础类型猜测你想要的字段类型映射.Elasticsearch ...
- WCF服务部署到IIS7.5
下面介绍如何把WCF服务部署到IIS: 为WCF服务创建.svc文件 我们知道,每一个ASP.NET Web服务都具有一个.asmx文本文件,客户端通过访问.asmx文件实现对相应Web服务的调用.与 ...
- MS10-046漏洞测试
这次用到MS10-046这个远程执行漏洞(Windows快捷方式LNK文件自动执行代码漏洞攻击) 命令: 1. msfconsole //启动MSF Metasploit 2. sear ...