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'  */*.h
real 0.38
user 0.00
sys 0.05 real > (user+sys)

2.时间相关函数

2.1 clock()

  • clock函数计算user time + system time
  • 单位时钟滴答
  • CLOCKS_PER_SECOND是1秒的时钟滴答数,可用sysconf函数获取,实际是个宏定义,不过不通系统定义可能是不同的
<time.h>
/* 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 <stdio.h>
#include <unistd.h> 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<int> v;
for(int a;in>>a;v.push_back(a));
cout<<v.size()<<endl;
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
cout<<endl;
clock_t t2=clock();
cout<<"TotalTime:"<<t2-t1<<"ms"<<endl;
} /*运行结果*/
Time to do 100000000 empty loops is 0.290000 seconds time ./test
real 0m0.915s
user 0m0.031s
sys 0m0.266s user+sys=0.297,正好等于clock的时间

2.2 times()

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

返回real time

<sys/times.h>

/* 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墙上时间
<time.h>
/* 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
<sys/time.h>

/* 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 <time.h> 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()的时间转换

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

<time.h>

/* 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 <xlocale.h> 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_GNU
extern 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()函数
    • 该函数对应墙上时间,进程切换也被统计进来了
    • 也没有太好的办法,只能多执行若干遍,求平均吧

linux应用态下的时间的更多相关文章

  1. linux kernel态下使用NEON对算法进行加速

    ARM处理器从cortex系列开始集成NEON处理单元,该单元可以简单理解为协处理器,专门为矩阵运算等算法设计,特别适用于图像.视频.音频处理等场景,应用也很广泛. 本文先对NEON处理单元进行简要介 ...

  2. Linux下长时间ping网络加时间戳并记录到文本

    Linux下长时间ping网络加时间戳并记录到文本   由于一些原因,比如需要检查网络之间是否存在掉包等问题,会长时间去ping一个地址,由于会输出大量的信息而且最好要有时间戳,因此我们可以使用简单的 ...

  3. Linux下精确控制时间的函数

    Linux下精确控制时间的函数 在测试程序接口运行时间的时候,常用time,gettimeofday等函数,但是这些函数在程序执行的时候是耗费时间的,如果仅仅测试时间还行,但是如果程序中用到时间控制类 ...

  4. Linux下设置时间

    Linux下设置时间 提供两种最根本有效的方式,就是更改时区.这里以更改为国内上海时间例子,其他地方时区同理. 方法一 备份文件 mv /etc/localtime /etc/localtime.ba ...

  5. Linux下系统时间函数、DST等相关问题总结(转)

    Linux下系统时间函数.DST等相关问题总结 下面这个结构体存储了跟时区相关的位移量(offset)以及是否存在DST等信息,根据所在的时区信息,很容易找到系统时间与UTC时间之间的时区偏移,另外根 ...

  6. linux环境下的时间编程

    Linux下提供了丰富的api以供开发者们处理和时间相关的问题.然而这些接口看似各自为政实则有有着千丝万缕的联系,在学习和时间中引发了各种各样的混乱.因此时间处理成为了许多Linux开发者的梦魇,遇到 ...

  7. Linux用户态和内核态

    究竟什么是用户态,什么是内核态,这两个基本概念以前一直理解得不是很清楚,根本原因个人觉得是在于因为大部分时候我们在写程序时关注的重点和着眼的角度放在了实现的功能和代码的逻辑性上,先看一个例子: 1)例 ...

  8. linux 用户态 内核态

    http://blog.chinaunix.net/uid-1829236-id-3182279.html 究竟什么是用户态,什么是内核态,这两个基本概念以前一直理解得不是很清楚,根本原因个人觉得是在 ...

  9. Linux多线程编程和Linux 2.6下的NPTL

    Linux多线程编程和Linux 2.6下的NPTL 在Linux 上,从内核角度而言,基本没有什么线程和进程的区别--大家都是进程.一个进程的多个线程只是多个特殊的进程他们虽然有各自的进程描述结构, ...

随机推荐

  1. Lucene全文检索学习笔记

    全文索引 介绍Lucene的作者:Lucene的贡献者Doug Cutting是 一位资深全文索引/检索专家,曾经是V-Twin搜索引擎(Apple的Copland操作系统的成就之一)的主要开发者,后 ...

  2. javascript获取年月日

    javascript获取年月日代码片段 function getNowDate() { var date = new Date(); var split = "-"; var ye ...

  3. 老男孩Python全栈开发(92天全)视频教程 自学笔记01

    day1课程目录: 开课介绍(1) 开课介绍(2) 开课介绍(3) 电脑简史(1) 电脑简史(2) 计算机结构 day1课程内容梳理: 导师介绍: Alex Li(金角大王):买了一辆特斯拉,喜欢姑娘 ...

  4. 【Java疑难杂症】有return的情况下try catch finally的执行顺序

    有这样一个问题,异常处理大家应该都不陌生,类似如下代码: public class Test { public static void main(String[] args) { int d1 = 0 ...

  5. 11. 配置ZooKeeper ensemble

    一个ZooKeeper集群或复制的ZooKeeper服务器集群应该优化配置,以避免出现脑裂(split-brain)等情况. 由于网络分割,同一ensemble的两个不同服务器可能构成领导者不一致,因 ...

  6. hotspot虚拟机的调试

    3这篇文章,怎么说呢.是踩了很多坑得出来了,也是在自己快要崩溃的时候得出来了的. 连续踩了差不多10来个小时的坑,还好是出来了. 这篇文章是调试虚拟机的,其实网上也能找到一些文章,但是每个人的环境不一 ...

  7. 【面试问题】——秋招面试中遇到的一些问题&思维导图&反思

    前言:秋招也跑了挺多的公司,虽然都是招Web前端,但是不同的公司,因为需求和目的不同,面试的考察点也是各不相同.我没有实习经验,只有自己学东学西比较杂也比较浅的一些知识积累可以用,这个过程我发现了自己 ...

  8. vue2.x利用脚手架快速构建项目并引入bootstrap、jquery

    要使用vue-cli脚手架搭建项目,首先需要安装node.js Node.js官网:https://nodejs.org/en/download/ 选择你对应的系统即可下载,下载完成后傻瓜式安装即可. ...

  9. C++杂分析

    class word{ public: word(){cout<<"word constructure \n";} word(int i){cout<<&q ...

  10. 51Nod--1085背包问题

    1085 背包问题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2--Wn(Wi为整 ...