【转】Linux时间函数之gettimeofday()函数之使用方法
原文网址:http://blog.csdn.net/tigerjibo/article/details/7039434
一.gettimeofday()函数的使用方法:
1.简介:
在C语言中可以使用函数gettimeofday()函数来得到时间。它的精度可以达到微妙
2.函数原型:
#include<sys/time.h>
int gettimeofday(struct timeval*tv,struct timezone *tz )
3.说明:
gettimeofday()会把目前的时间用tv 结构体返回,当地时区的信息则放到tz所指的结构中
4.结构体:
1>timeval
struct timeval{
long tv_sec;/*秒*/
long tv_usec;/*微妙*/
};
2>timezone 结构定义为:
struct timezone{
int tz_minuteswest;/*和greenwich 时间差了多少分钟*/
int tz_dsttime;/*type of DST correction*/
}
3>在gettimeofday()函数中tv或者tz都可以为空。如果为空则就不返回其对应的结构体。
4>函数执行成功后返回0,失败后返回-1,错误代码存于errno中。
5.程序实例:
#include<stdio.h>
#include<sys/time.h>
#include<unistd.h>
int main()
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv,&tz);
printf(“tv_sec:%d\n”,tv.tv_sec);
printf(“tv_usec:%d\n”,tv.tv_usec);
printf(“tz_minuteswest:%d\n”,tz.tz_minuteswest);
printf(“tz_dsttime:%d\n”,tz.tz_dsttime);
}
说明:在使用gettimeofday()函数时,第二个参数一般都为空,因为我们一般都只是为了获得当前时间,而不用获得timezone的数值
二.gettimeofday()函数的一个常用方法
在测试程序时,往往需要了解程序执行所需的时间,在Linux中可以使用函数gettimeofday来得到时间.
1.程序实例:
测试调用delya()函数所需执行的时间(单位为微妙)
#include<stdio.h>
#include<sys/time.h>
#include<unistd.h>
int delay(int time)
{
int i,j;
for(i =0;i<time;i++)
for(j=0;j<5000;j++)
;
}
int main()
{
struct timeval start;
struct timeval end;
unsigned long diff;
gettimeofday(&start,NULL);
delay(10);
gettimeofday(&end,NULL);
diff = 1000000 * (end.tv_sec-start.tv_sec)+ end.tv_usec-start.tv_usec;
printf(“thedifference is %ld\n”,diff);
return 0;
}
说明:
使用该方法就可以检测出调用delay()函数所使用的时间
【转】Linux时间函数之gettimeofday()函数之使用方法的更多相关文章
- Linux时间函数之gettimeofday()函数之使用方法
1.简介: 在C语言中可以使用函数gettimeofday()函数来得到时间.它的精度可以达到微妙 2.函数原型: #include<sys/time.h> int gettimeofda ...
- linux中获取系统时间 gettimeofday函数
linux的man页中对gettimeofday函数的说明中,有这样一个说明: $ man gettimeofday DESCRIPTION The functions gettimeof ...
- Linux时间子系统之(三):用户空间接口函数
专题文档汇总目录 Notes:用户空间时间相关接口函数: 类型 API 精度 说明 时间 time stime time_t 精度为秒级 逐渐要被淘汰.需要定义__ARCH_WANT_SYS_TIME ...
- Linux时间子系统(三) 用户空间接口函数
一.前言 从应用程序的角度看,内核需要提供的和时间相关的服务有三种: 1.和系统时间相关的服务.例如,在向数据库写入一条记录的时候,需要记录操作时间(何年何月何日何时). 2.让进程睡眠一段时间 3. ...
- Linux时间函数
一.时间编程 1.核心理论 (1).时间类型 2.函数学习 (1).获取日历时间 函数名:time 函数原型:time_t time(time_t *t) 函数功能:获取当前日历时间 所属头文件:&l ...
- gettimeofday()函数来得到时间
gettimeofday()函数的使用方法: 1.简介: 在C语言中可以使用函数gettimeofday()函数来得到时间.它的精度可以达到微妙 2.函数原型: #include<sys/tim ...
- Linux时间时区详解与常用时间函数
时间与时区 整个地球分为二十四时区,每个时区都有自己的本地时间. Ø UTC时间 与 GMT时间 我们可以认为格林威治时间就是时间协调时间(GMT = UTC),格林威治时间和UTC时间都用秒数来计 ...
- Windows及Linux平台下的计时函数总结
本文对Windows及Linux平台下常用的计时函数进行总结,包括精度为秒.毫秒.微秒三种精度的各种函数.比如Window平台下特有的Windows API函数GetTickCount().timeG ...
- Linux-C语言中gettimeofday()函数的使用方法(转载)
1.简介: 在C语言中可以使用函数gettimeofday()函数来得到时间.它的精度可以达到微妙 2.函数原型: #include<sys/time.h> int gettimeofda ...
随机推荐
- Consumer closed input channel or an error occurred. events=0x8 channel is unrecoverably broken and will be disposed(待解决)
跟文件读取有关?关闭文件读取试试. 有可能是读取文件 出现报错
- Lua print on the same line
In Pascal, I have write and writeln. Apparently Lua's print is similar to writeln of Pascal. Do we h ...
- 2013 Multi-University Training Contest 2 Balls Rearrangement
先算出lcm(a,b),如果lcm>=n,则直接暴力解决:否则分段,求出0-lcm内的+0-n%lcm内的值. 再就是连续相同的一起计算!! #include<iostream> # ...
- Project Euler 94:Almost equilateral triangles 几乎等边的三角形
Almost equilateral triangles It is easily proved that no equilateral triangle exists with integral l ...
- Nginx安装部署
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev ...
- 关于delphi Assigned
1. 根据 Delphi 指令参考手册中 说明: Assigned 函式在参数不为 nil 时传回 True, 表示指针已经指到某个内存地址,这个内存地址可能是一个对象地首地址,也可能在函数或过程中, ...
- IP地址字符串与BigInteger的转换
/** * Copyright (c) 2010, 新浪网支付中心 * All rights reserved. * * Java IP地址字符串与BigInteger的转换, * ...
- python流程控制语句 ifelse - 1
考点:条件判断语句if-elif 代码: #! /usr/bin/python print ('\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n') p ...
- PHP大神的十大优良习惯
概述:通往PHP大神的道路上,应该保持优良的传统和习惯. 1.多阅读手册和源代码 没什么比阅读手册更值得强调的事了–仅仅通过阅读手册你就可以学习到很多东西,特别是很多有关于字符串和数组的函数.就在这些 ...
- YUV到RGB的转换
以下内容来源于网络,下面三个链接里的内容是比较好的,感谢博主的分享. http://blog.csdn.net/housisong/article/details/1859084 http://blo ...