| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2188 人关注过本帖
标题:求科普:gettimeofday函数的使用
只看楼主 加入收藏
S酱紫
Rank: 2
等 级:论坛游民
威 望:3
帖 子:25
专家分:20
注 册:2019-1-2
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
求科普:gettimeofday函数的使用
网上搜了获取系统时间的方法,搜了很久才查到用gettimeofday函数,但奈何网上搜到的资料比较复杂,里面涉及到的很多参数不太理解,搜到了以下几个例子,但不太理解里面的使用方法,不知道有没有大神能用简单易懂的方法讲下简单使用方法(本人萌新,尽量不要太复杂,理解会用就好)

程序代码:
#include <sys/time.h>
void f()
{

 //...
}
int main()
{

 struct timeval t1, t2;

 gettimeofday(&t1, NULL);

 f();

 gettimeofday(&t2, NULL);

 //那么函数f运行所花的时间为

 //deltaT = (t2.tv_sec-t1.tv_sec) * 1000000 + t2.tv_usec-t1.tv_usec 微秒
 return 0;
}


程序代码:
#include <sys/time.h>
#include <unistd.h>
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);
}


程序代码:
#include<time.h>
#include<stdio.h>
#define MILLION 1000000
int main(void)
{
        struct timespec tpstart;
        struct timespec tpend;
        long timedif;
        gettimeofday(&tpstart, NULL);
         gettimeofday(&tpend, NULL);
        timedif = MILLION*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000;
        fprintf(stdout, "it took %ld microseconds\n", timedif);
        return 0;
}


有些地方需要大神讲解一下:例如第一第二个代码用<sys/time.h>第三个代码却用<time.h>不太理解二者有什么区别……还有,这三段代码有没有什么联系和区别?

能讲明白gettimeofday这个函数的简单用法就可以啦
搜索更多相关主题的帖子: 函数 方法 time include struct 
2019-03-08 09:43
wp2319570
Rank: 2
等 级:论坛游民
帖 子:30
专家分:66
注 册:2019-3-4
收藏
得分:5 
看形状类似 #include <sys/time.h>  这样中间带/的 大都是linux下的源码  windows不支持这样写
2019-03-08 10:05
tisyang
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:132
专家分:737
注 册:2011-5-7
收藏
得分:15 
时间是相对一个纪元(Epoch)开始累积的计数,单位是秒,在C里就用 time_t 这个类型来表达,实质上就是一个整数,只能表达整秒精度。
对于真实时间,在系统里的使用的是 UNIX Epoch 纪元,即 1970-1-1 00:00:00 (UTC/GMT时区)开始过后的秒数,这个时间是唯一的,不会因为时区不同而变化。
struct timeval/ struct timespec 都是支持更精度时间的结构,timeval 带有微秒,timespec 带有纳秒。
以下代码均为获取当前时间的函数调用:
程序代码:
/*******/
/* time 函数是 C 标准函数,所有平台都可以使用 */
time_t now;
time(&now);
/*******/
/* gettimeofday 是LINUX/UNIX下函数  */
struct timeval now;
gettimeofday(&now, NULL);
/*******/
/* clock_gettime 是 POSIX 定义函数,UNIX/LINUX 都可以使用, windows 下 MINGW 有移植 */
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now); 

time_t/struct timeval/struct timespec 都只是表达基于纪元的计数,人类读取很难理解,所以就需要转换到可读的时间。
转换时间使用的是 gmtime/localtime ,转换结果是一个 struct tm 结构,结构里是公元计时的年份/月份/日/时/分/秒,其定义如下:
程序代码:
           struct tm {
               int tm_sec;    /* Seconds (0-60) */
               int tm_min;    /* Minutes (0-59) */
               int tm_hour;   /* Hours (0-23) */
               int tm_mday;   /* Day of the month (1-31) */
               int tm_mon;    /* Month (0-11) */
               int tm_year;   /* Year - 1900 */
               int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
               int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
               int tm_isdst;  /* Daylight saving time */
           };

gmtime 是将时间转为 UTC/GMT 时间,localtime 是将时间转为当地时间(受时区影响)。
将时间转换为字符串的函数是 strftime。
具体函数用法可以参阅文档 https://www.


C++ 用无参数构造函数生成对象时候请勿在构造函数后添加无用的那一对括号,否则有可能会被当成函数声明而忽略,嗯,栈上构建的时候就是这样。
2019-03-08 15:49
快速回复:求科普:gettimeofday函数的使用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018903 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved