专题实战时间

Linux中时间的几个基本概念:
     UTC时间:世界统一时间,世界各地的时间以这个为基准。

     UNIX 纪元时间:Unix time 是指从1970 年1 月1 日00:00:00 UTC 开始所经过的秒数

    格林尼治标准时(GMT)时间:是指太阳经过英国格林尼治的时间

    机器日历时间:对于Linux 来说,机器日历时间是UNIX 纪元时间

 

时间的调用

函数time_t time(time_t *t)在头文件“#include <time.h>”中。
          参数1:time_t 类型,以秒为单位,如果有参数则数据传送到time_t *t 中。
          返回值:如果参数为NULL 会返回time_t 类型;出现错误返回-1。

    其中的时间类型time_t,它是一个long 类型,及其机器日历时间,在头文件time.h 中定义。

gettime.c

#include<stdio.h>
#include<time.h>

int main(void)
{
	time_t timep;
	time(&timep);
	printf("UTC time: 0x%08x\n",timep);
	timep = time(NULL)
	printf("UTC time: 0x%08x\n",timep);

	return 0;
}

         使用了两种方式返回时间,一种写入参数,一种写入NULL。

          将文件交叉编译 移至开发板运行 即可看到实验效果(“arm-none-linux-gnueabi-gcc -o gettime gettime.c -static”)

 

时间的转换:

常用的时间结构体tm

exchangetime.c

#include<stdio.h>
#include<time.h>

int main(void)
{
	time_t timep;
	struct tm *tblock;
	
	time(&timep);
	printf("ctime/timep is %s",ctime(&timep));
	printf("asctime is %s",asctime(gmtime(&timep)));
	
	tblock = localtime(&timep);
	printf("localhost is: %s",asctime(tblock));
	printf("localtime is %s",ctime(&timep));
	return 0;
	
}

再将文件 编译 移至开发板运行  “arm-none-linux-gnueabi-gcc -o exchangtime exchangtime.c -static”

输出:

处理器性能的测试


int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv, const struct timezone *tz);

上面两个时间函数在头文件“#include <sys/time.h>”中。

上面获取的时间精度达到了微妙,比time 函数获取的时间精度要高6 个数量级。这个精度就可以用于计算代码执行时间了。

实验代码:precisiontime.c

#include<stdio.h>
#include<time.h>
#include<sys/time.h>

void function()
{
	unsigned int i,j;
	double y;
	for(i = 0; i < 1000; i++)
	for(j = 0; j < 1000; j++)
	y = i/(j+1)
}

int main(void)
{
	struct timeval tpstart,tpend;
	float timeuse;
	
	gettimeofday(&tpstart,NULL);
	function();
	
	gettimeofday(&tpend,NULL);
	timeuse = 1000000*(tpend.tv_sec - tpstart.tv_sec) + (tpend.tv_usec - tpstart.tv_usec);
	timeuse /= 1000000;
	
	printf("Used Time:%f\n",timeuse);
}

     编译,将可执行文件移至开发板              “arm-none-linux-gnueabi-gcc -o precisiontime precisiontime.c -static”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值