下面叙述两方面的东西。第一部分,介绍自己写的两个获取时间的示例,这是比较常用的用法;第二部分,各种时间函数的用法汇总,来自网上,已经无法追溯出处了。搜索了下,在以下博客《c语言中时间的获取》中有类似内容。
http://hi.baidu.com/greatren518/blog/item/5c5faf5413b13352d00906e2.html
第一部分
示例1:使用time()和localtime()来统计某一段代码的执行时间,这里只要求精确到分钟。其实使用这两个函数可以精确到秒。
/*get_time0.c*/
#include <stdio.h>
#include <time.h>
#define SLEEPING_TIME 65
void function_B()
{
printf("sleeping %ds\r\n", SLEEPING_TIME);
sleep(SLEEPING_TIME);
}
int main()
{
time_t time_current;
time_t time_start;
struct tm *ptime_start;
struct tm *ptime_current;
int interval=0;
time(&time_start);
ptime_start = localtime(&time_start);
function_B();
time(&time_current);
ptime_current = localtime(&time_current);
interval = ((ptime_current->tm_hour) - (ptime_start->tm_hour)) * 60 + ((ptime_current->tm_min) - (ptime_start->tm_min));
printf("interval =%d m\r\n", interval);
return 0;
}
编译:
# gcc -o get_time0 get_time0.c
执行结果:
# ./get_time0
sleeping 65s
interval =0 m
发现时间间隔interval 居然为0. 怎么回事呢?看看加了以下打印的get_time1.c
示例2:在 get_time0.c 上 加了些打印。
/*get_time1.c*/
#include <stdio.h>#include <time.h>
#define SLEEPING_TIME 65
void function_B()
{
printf("sleeping %ds\r\n", SLEEPING_TIME);
sleep(SLEEPING_TIME);
}
int main()
{
time_t time_current;
time_t time_start;
struct tm *ptime_start;
struct tm *ptime_current;
int interval=0;
time(&time_start);
ptime_start = localtime(&time_start);
printf("start_hour =%dh, start_minute=%dm\r\n", ptime_start->tm_hour, ptime_start->tm_min);
function_B();
time(&time_current);
ptime_current = localtime(&time_current);
printf("current_hour =%dh, current_minute=%dm\r\n", ptime_current->tm_hour, ptime_current->tm_min);
printf("t_h_start=%d\r\n", ptime_start->tm_hour);
printf("t_h_current=%d\r\n", ptime_current->tm_hour);
printf("t_m_start=%d\r\n", ptime_start->tm_min);
printf("t_m_current=%d\r\n", ptime_current->tm_min);
interval = ((ptime_current->tm_hour) - (ptime_start->tm_hour)) * 60 + ((ptime_current->tm_min) - (ptime_start->tm_min));
printf("interval =%d m\r\n", interval);
return 0;
}
编译:
# gcc -o get_time1 get_time1.c
执行结果:
# ./get_time1
start_hour =17h, start_minute=21m
sleeping 65s
current_hour =17h, current_minute=22m
t_h_start=17
t_h_current=17
t_m_start=22
t_m_current=22
interval =0 m
从结果可以看出,在第二次执行localtime()后,指针ptime_start 和 ptime_current所指向的区域的值变得一样了。为什么会这样呢?这是由于localtime()完成对输入时间(s)的格式转换后,返回的是一个指针,这个指针的内存是在localtime()内部分配的。我猜测是一个静态局部变量,两次使用localtime()返回的指针都指向这个静态局部变量,而第二使用后的值把第一次的覆盖了。
其实,这里如果真的是想统计时间间隔的话,直接将time_start 和 time_current 相减就可以了。因为使用time()获得时间本身就是从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。
interval = time_current - time_start; /*second*/
之所以大费周章,是想说明localtime()的用法和使用localtime()转换时间格式的时候要当心。
使用time()获取时间 和localtime()转换时间格式,只能精确到秒。如果需要更精确一点,可以使用gettimeofday(),这个时间函数可以精确到us。见示例3.
示例3:使用gettimeofday()获取时间间隔,精确到ms(可以精确到us)。
/*get_time2.c*/
#include <stdio.h>
#include <sys/time.h>
#define SLEEPING_TIME 2
void function_A()
{
printf("sleeping %ds\r\n", SLEEPING_TIME);
sleep(SLEEPING_TIME);
}
int main()
{
struct timeval start, end;
int interval;
gettimeofday(&start, NULL);
function_A();
gettimeofday(&end, NULL);
interval = 1000000*(end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);/*us*/
printf("interval = %fms\n", interval/1000.0);
}
编译:
# gcc -o get_time2 get_time2.c
执行结果:
# ./ge