C语言获取程序运行时间

一 time命令

1 示例

运行时前加time。例如:

#time  ./test
real    0m2.815s
user    0m0.000s
sys     0m0.016s

2 常用参数

  time命令常用于测量一个进程的运行时间,以及测量内存、I/O等的使用情况,注意不是用来显示和修改系统时间的(这是date命令干的事情)。一个程序在运行时使用的系统资源通常包括CPU、Memory和I/O等,其中CPU资源的统计包括实际使用时间(real time)、用户态使用时间(the process spent in user mode)、内核态使用时间(the process spent in kernel mode)。但是简单的使用time命令并不能得到内存和I/O的统计数据。
  time命令最常用的使用方式就是在其后面直接跟上命令和参数:

time  []

  在命令执行完成之后就会打印出CPU的使用情况:

real    0m5.064s      <== 实际使用时间(real time) 
user    0m0.020s     <== 用户态使用时间(the process spent in user mode) 
sys     0m0.040s      <== 内核态使用时间(the process spent in kernel mode)

  time命令跟上-p参数可以只打印时间数值(秒数),不打印单位。

3 两个time

# type -a time 
time is a shell keyword
time is /usr/bin/time

  通过这条命令发现常用的time其实是一个Shell关键字,还有一个外部命令/usr/bin/time

# /usr/bin/time ./a.out
ret: 987 #运行结果,不用在意
time: 3002509 us #函数计时
0.00user 0.00system 0:03.00elapsed 0%CPU (0avgtext+0avgdata 584maxresident)k
0inputs+0outputs (0major+173minor)pagefaults 0swaps

  信息混乱,使用- v参数:

# /usr/bin/time -v ./a.out
ret: 987
time: 3003543 us
        Command being timed: "./a.out"
        User time (seconds): 0.00
        System time (seconds): 0.00
        Percent of CPU this job got: 0%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:03.01
        Average shared text size (kbytes): 0
        Average unshared data size (kbytes): 0
        Average stack size (kbytes): 0
        Average total size (kbytes): 0
        Maximum resident set size (kbytes): 584
        Average resident set size (kbytes): 0
        Major (requiring I/O) page faults: 0
        Minor (reclaiming a frame) page faults: 174
        Voluntary context switches: 0
        Involuntary context switches: 0
        Swaps: 0
        File system inputs: 0
        File system outputs: 0
        Socket messages sent: 0
        Socket messages received: 0
        Signals delivered: 0
        Page size (bytes): 4096
        Exit status: 0

  
  以下内容来自:我使用过的Linux命令之time - 测定一个命令的资源使用情况

二 函数获取

#include 
#include 

unsigned long long fibnacci(int n)
{
	if(n == 1 || n == 2)
		return 1;
	return (fibnacci(n-1) + fibnacci(n-2));
}

int main()
{
	struct timeval start, end;
	gettimeofday(&start, NULL);//传空表示按内核时区进行计算;记录下系统当前时间给start,下面写入需要计算时间的程序

	unsigned long long ret = fibnacci(45);//斐波那契数列中第45个元素(测试的程序)
	printf("ret: %llu\n", ret);//需要测试的程序

	gettimeofday(&end, NULL);//记录下系统当前时间给end,此时程序结束。

	printf("time: %lu us\n", (end.tv_sec - start.tv_sec)*1000000
			+ (end.tv_usec - start.tv_usec));

	//结束时间减开始时间。该结构体中,tv_sec存  入秒;tv_usec存入微秒。

	//结果输出微秒为单位
	return 0; 
}

不发表关于时间在于物理以及哲学的定义。。。。。。。(没这个本事(此处略去一万字))

基准

  时间有绝对和相对的概念。例如1小时候我们就去吃饭。这里就是一个相对时间的概念,1小时是相对当前时间点而言。1949年10月1日,中华人民共和国成立,这里的时间就是绝对时间。当然,实际上,这里的绝对时间也是相对时间,只不过所有地球上的人都使用同一个基准点的时候,这个时间表示就是一个普遍适用的绝对时间了。
  对于一个系统而言,需要定义一个epoch,所有的时间表示是基于这个基准点的,对于linux而言,采用了和unix epoch一样的时间点:1970年1月1日0点0分0秒(UTC)。NTP协议使用的基准点是:1900年1月1日0点0分0秒(UTC)。GPS系统使用的基准点是:1980年1月6日0点0分0秒(UTC)。每个系统都可以根据自己的逻辑定义自己epoch,例如unix epoch的基准点是因为unix操作系统是在1970年左右成型的。

时间戳

  时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总毫秒数。通俗的讲, 时间戳是一份能够表示一份数据在一个特定时间点已经存在的完整的可验证的数据。 它的提出主要是为用户提供一份电子证据, 以证明用户的某些数据的产生时间。 在实际应用上, 它可以使用在包括电子商务、 金融活动的各个方面, 尤其可以用来支撑公开密钥基础设施的 “不可否认” 服务。

绝对时间

  字面意思,此时此刻的时间。

相对时间

参考文章

  linux 相对时间:https://blog.csdn.net/u011244446/article/details/46828759
  Linux时间子系统之(一):时间的基本概念:http://www.wowotech.net/timer_subsystem/time_concept.html
  时间子系统:http://www.wowotech.net/sort/timer_subsystem
  

你可能感兴趣的:(OS)