linux应用态下的时间
发布日期:2021-05-19 03:28:00 浏览次数:15 分类:博客文章

本文共 5890 字,大约阅读时间需要 19 分钟。

1.时间值

1.1 日历时间(UTC)

 该值是自1 9 7 0年1月1日0 0 : 0 0 : 0 0以来国际标准时间( U T C)所经过的秒数累计值(早期的手册称 U T C为格林尼治标准时间) ,系统中用time_t类型表示。

1.2 进程时间

  也叫CPU时间,用来度量占用的CPU资源。

  单位:时钟滴答,系统每秒的滴答数可以通过sysconf获取,曾经取值50/60/100等,用clock_t类型表示;就算1000,精度才1ms,不适用高精度测量

  三个进程时间值:

  • 时钟时间real,也叫墙上时间,时钟时间是整个进程的实际执行时间,从进程开始到进程结束,包含期间进程切换,其他进程的执行时间。此时间跟进程的数量有关,例如同样一个进程,如果系统中有3个和10个其他进程和的情况下,时钟时间是不一样的,即跟系统的负荷有关系。
  • 用户时间user,该进程处于用户态时运行的CPU时间
  • 系统时间sys,系统时间是该进程进入内核态以后的CPU运行时间

一般情况:read >= (user+sys),多线程并行执行时是例外

:/work/platform-zynq/linux-xlnx-repo-smp$ time -p grep 'xilinx'  */*.hreal 0.38user 0.00sys 0.05real > (user+sys)

2.时间相关函数

2.1 clock()

  • clock函数计算user time + system time
  • 单位时钟滴答
  • CLOCKS_PER_SECOND是1秒的时钟滴答数,可用sysconf函数获取,实际是个宏定义,不过不通系统定义可能是不同的
/* Time used by the program so far (user time + system time). The result / CLOCKS_PER_SECOND is program time in seconds. */extern clock_t clock (void) __THROW;

获取CLOCKS_PER_SECOND的值:

#include 
#include
int main(int argc, char *argv[]){ printf("CLOCKS_PER_SECOND=%d\r\n",sysconf(_SC_CLK_TCK)); return 0;}在MPSOC开发板上中执行的结果为:CLOCKS_PER_SECOND=100

精度太低了,10ms

例子:

int main(int argc, char **argv){        clock_t t1=clock();    ifstream in("data.txt");    vector
v; for(int a;in>>a;v.push_back(a)); cout<
<

2.2 times()

clock()函数的增强版,可获取real、usr、sys time,跟命令time差不多。

返回real time

/* Structure describing CPU time used by a process and its children. */struct tms { clock_t tms_utime; /* User CPU time. */ clock_t tms_stime; /* System CPU time. */ clock_t tms_cutime; /* User CPU time of dead children. */ clock_t tms_cstime; /* System CPU time of dead children. */ };/* Store the CPU time used by this process and all its dead children (and their dead children) in BUFFER. Return the elapsed real time, or (clock_t) -1 for errors. All times are in CLK_TCKths of a second. */extern clock_t times (struct tms *__buffer) __THROW;

2.3 time()

  • 获取系统时间,1 9 7 0年1月1日0 0 : 0 0 : 0 0以来国际标准时间( U T C)所经过的秒数累计值
  • 对应系统时间,若用此函数作为start和end,则计算出来的时间对应real墙上时间
/* Return the current time and put it in *TIMER if TIMER is not NULL. */extern time_t time (time_t *__timer) __THROW;

2.4 gettimeofday()

  • 跟time()差不多,但比time()精度高,可达到us级
  • 返回的时间是 从今日凌晨到现在的时间差,绝对值
  • 用start,end方式得到的差值对应real time
/* Get the current time of day and timezone information, putting it into *TV and *TZ. If TZ is NULL, *TZ is not filled. Returns 0 on success, -1 on errors. NOTE: This form of timezone information is obsolete. Use the functions and variables declared in
instead. */extern int gettimeofday (struct timeval *__restrict __tv, __timezone_ptr_t __tz) __THROW __nonnull ((1)); /* A time value that is accurate to the nearest microsecond but also has a range of years. */struct timeval { __time_t tv_sec; /* Seconds. */ __suseconds_t tv_usec; /* Microseconds. */ };/*__tz填入NULL即可*/

2.5 time()/gettimeofday()的时间转换

这两个函数获取的时间不易读,可通过若干函数转换成人们易读的方式。

/* Return the `time_t' representation of TP and normalize TP. */extern time_t mktime (struct tm *__tp) __THROW;/* Format TP into S according to FORMAT. Write no more than MAXSIZE characters and return the number of characters written, or 0 if it would exceed MAXSIZE. */extern size_t strftime (char *__restrict __s, size_t __maxsize, const char *__restrict __format, const struct tm *__restrict __tp) __THROW;__END_NAMESPACE_STD# ifdef __USE_XOPEN/* Parse S according to FORMAT and store binary time information in TP. The return value is a pointer to the first unparsed character in S. */extern char *strptime (const char *__restrict __s, const char *__restrict __fmt, struct tm *__tp) __THROW;# endif# ifdef __USE_XOPEN2K8/* Similar to the two functions above but take the information from the provided locale and not the global locale. */# include
extern size_t strftime_l (char *__restrict __s, size_t __maxsize, const char *__restrict __format, const struct tm *__restrict __tp, __locale_t __loc) __THROW;# endif# ifdef __USE_GNUextern char *strptime_l (const char *__restrict __s, const char *__restrict __fmt, struct tm *__tp, __locale_t __loc) __THROW;# endif__BEGIN_NAMESPACE_STD/* Return the `struct tm' representation of *TIMER in Universal Coordinated Time (aka Greenwich Mean Time). */extern struct tm *gmtime (const time_t *__timer) __THROW;/* Return the `struct tm' representation of *TIMER in the local timezone. */extern struct tm *localtime (const time_t *__timer) __THROW;__END_NAMESPACE_STD# ifdef __USE_POSIX/* Return the `struct tm' representation of *TIMER in UTC, using *TP to store the result. */extern struct tm *gmtime_r (const time_t *__restrict __timer, struct tm *__restrict __tp) __THROW;/* Return the `struct tm' representation of *TIMER in local time, using *TP to store the result. */extern struct tm *localtime_r (const time_t *__restrict __timer, struct tm *__restrict __tp) __THROW;# endif /* POSIX */__BEGIN_NAMESPACE_STD/* Return a string of the form "Day Mon dd hh:mm:ss yyyy\n" that is the representation of TP in this format. */extern char *asctime (const struct tm *__tp) __THROW;/* Equivalent to `asctime (localtime (timer))'. */extern char *ctime (const time_t *__timer) __THROW;__END_NAMESPACE_STD# ifdef __USE_POSIX/* Reentrant versions of the above functions. *//* Return in BUF a string of the form "Day Mon dd hh:mm:ss yyyy\n" that is the representation of TP in this format. */extern char *asctime_r (const struct tm *__restrict __tp, char *__restrict __buf) __THROW;/* Equivalent to `asctime_r (localtime_r (timer, *TMP*), buf)'. */extern char *ctime_r (const time_t *__restrict __timer, char *__restrict __buf) __THROW;# endif /* POSIX */

3.测量某段代码执行时间的函数选择

  • us级函数执行时间测量,使用clock_t类型的函数肯定无法满足要求;
  • 只能用gettimeofday()函数
    • 该函数对应墙上时间,进程切换也被统计进来了
    • 也没有太好的办法,只能多执行若干遍,求平均吧
上一篇:make工程管理器
下一篇:MPSOC之9——host、embeded间tftp、nfs、ftp环境搭建

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月14日 17时22分43秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

cytoscape安装java_Cytoscape史上最全攻略 2023-01-24
c语言程序设计年历显示,C语言程序设计报告《万年历》.doc 2023-01-24
C语言程序设计梁海英答案,1.5 习题 2023-01-24
c语言编写单片机中断,C语言AVR单片机中断程序写法 2023-01-24
#pragma region、{} 2023-01-24
ddr2的上电顺序_S5PV210 DDR2初始化 28个步骤总结 2023-01-24
deque stack java_「集合系列」- 初探 java 集合框架图 2023-01-24
easyexcel 导出 代码翻译converter_【starter推荐】简单高效Excel 导出工具 2023-01-24
echarts 如何在一条柱形显示两个数字_干货 | 如何快速制作数据地图?让你的可视化逼格再高一级!... 2023-01-24
eclipse设置utf8编码_记住没:永远不要在 MySQL 中使用 UTF8 2023-01-24
eclipse里source的快捷方法_Eclipse快捷键/快捷操作汇总 2023-01-24
elasticsearch 查询_Elasticsearch地理信息存储及查询之Geo_Point 2023-01-24
embedding层_【预估排序】Embedding+MLP: 深度学习预估排序通用框架(一) 2023-01-24
excel中最常用的30个函数_Excel玩转数据分析常用的43个函数! 2023-01-24
flink sql设置并行度_Flink 参数配置和常见参数调优 2023-01-24
go 字符串替换_Go 每日一库之 quicktemplate 2023-01-24
hex editor neo下载_口袋妖怪爆焰黑手机版下载-口袋妖怪爆焰黑手游下载v4.3.0 安卓版... 2023-01-24
hibernate mysql 关联查询_spring-boot hibernate 双向关联查询的坑 2023-01-24
hive 建表_sqoop的使用之导入到hive和mysql 2023-01-24
hp工作站z8装Linux,惠普Z8G4双路最小工作站 2023-01-24