栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

linux C语言中的时间函数

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux C语言中的时间函数

1.time

#include 

time_t t_start,  t_end;
int duration;

t_start = time(NULL);  //单位为s,秒
sleep(1);
t_end = time(NULL); //单位为s
duration = difftime(t_end, t_start);
printf("time duration: %dsn", duration);

2.gettimeofday

#include 

struct timeval start, end;
float time_use;

gettimeofday(&start, NULL); //最小单位为us,微秒
sleep(1);
gettimeofday(&end, NULL);   //us
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %.10fsn",time_use/1000000.0);

3.clock_gettime

#include 

struct timespec tpstart;
struct timespec tpend;
long l_timedif;
double timedif;

clock_gettime(CLOCK_MONOTONIC, &tpstart);   //最小单位为ns,纳秒
sleep(1);
clock_gettime(CLOCK_MONOTONIC, &tpend);

l_timedif = 1000000*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000; //us,微秒
timedif = (double)l_timedif/1000000.0;  //s,秒
printf("timedif: %fsn", timedif);

参考:

time()_百度百科 (baidu.com)

clock_gettime_百度百科 (baidu.com)

gettimeofday_百度百科 (baidu.com)

C++用clock()统计程序运行时间时的坑_litart coder的博客-CSDN博客_c++ clock 不准

difftime_百度百科 (baidu.com)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/917244.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号