linux c计算时间差值,获取时间和计算时间差的几种方法总结,时间差几种方法...

获取时间和计算时间差的几种方法总结,时间差几种方法

一、标准C和C++都可用

1、获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( time_t timer1, time_t timer0 )。 精确到秒。

测试程序如下:

1 #include

2 #include

3 intmain()4 {5 time_t start ,end ;6 doublecost;7 time(&start);8 sleep(1);9 time(&end);10 cost=difftime(end,start);11 printf("%f/n",cost);12 return 0;13 }

本程序在fedora9测试通过。

关于代码中的sleep函数,需要注意的是:

1)在windows下,为Sleep函数,且包含windows.h

2)关于sleep中的数,在Windows和Linux下1000代表的含义并不相同,Windows下的表示1000毫秒,也就是1秒钟;Linux下表示1000秒,Linux下使用毫秒级别的函数可以使用usleep。

2、clock_t clock(),clock()

获取的是计算机启动后的时间间隔,得到的是CPU时间,精确到1/CLOCKS_PER_SEC秒。

测试程序如下:

1 #include

2 #include

3 intmain()4 {5 doublestart,end,cost;6 start=clock();7 sleep(1);8 end=clock();9 cost=end-start;10 printf("%f/n",cost);11 return 0;12 }

二、C++中(此处针对windows环境,标准c中则linux和windows都可以)

1、GetTickCount()

调用函数需包含windows.h。得到的是系统运行的时间 精确到毫秒,测试程序如下:

1 #include

2 #include

3 using namespacestd;4 intmain()5 {6 double start =GetTickCount();7 Sleep(1000);8 double end=GetTickCount();9 cout << "GetTickCount:" << end-start <

2、GetLocalTime()

获得的是结构体保存的year,month等信息。而C语言time函数获得是从1970年1月1日0时0分0秒到此时的秒数。需要gmtime函数转换为常用的日历(返回的是世界时间,要显示常用的时间,则为localtime函数)。

在c语言中,保存常用日历的结构体为struct tm,包含在time.h中,c++语言为SYSTEMTIME结构体,包含在winbase.h(编程包含windows.h即可)。当然,精度肯定为秒了。

测试程序如下:

1 #include

2 #include

3 using namespacestd;4 intmain()5 {6 SYSTEMTIME start; //windows.h中

7 GetLocalTime(&start);//time.h的tm结构体一样的效果

8 cout<< start.year <

c语言的gmtime方法的示范代码如下:

#include #include#include

intmain()

{struct tm *tm_ptr;

time_t the_time;

(void) time(&the_time);

tm_ptr= gmtime(&the_time);

printf("Raw time is %ld/n", the_time);

printf("gmtime gives:/n");

printf("date: %02d/%02d/%02d/n",

tm_ptr->tm_year, tm_ptr->tm_mon+1, tm_ptr->tm_mday);

printf("time: %02d:%02d:%02d/n",

tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec);

exit(0);

}

另外,c语言有类似于GetLocalTime方法的函数ctime()。

对localtime(),原型为:struct tm *localtime(const time_t *timep);将测试程序的gmtime改为localtime,则可以看到输出的时间为争取时间和日期了。为了更友好的得到时间和日期,像date那样输出,可以用asctime或ctime函数,原型:char  *ctime(const time_t  *timeval);测试代码如下:

1 #include

2 #include

3 #include

4 intmain()5 {6 time_t the_time;7 time(&the_time);8 printf("The date is : %s /n" , ctime(&the_time));9 exit(0);10 }

3、要获取高精度时间,可以使用

BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)获取系统的计数器的频率

BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)获取计数器的值

然后用两次计数器的差除以Frequency就得到时间。

测试程序如下:

1 #include

2 #include

3 using namespacestd;4 intmain()5 {6 LARGE_INTEGER m_nFreq;7 LARGE_INTEGER m_nBeginTime;8 LARGE_INTEGER nEndTime;9 QueryPerformanceFrequency(&m_nFreq); //获取时钟周期

10 QueryPerformanceCounter(&m_nBeginTime); //获取时钟计数

11 Sleep(100);12 QueryPerformanceCounter(&nEndTime);13 cout << (double)(nEndTime.QuadPart-m_nBeginTime.QuadPart)*1000/m_nFreq.QuadPart <

需要注意的就是结果需要强制转换为double,不然会得到如下错误:<< is ambiguous。

4、timeGetTime()。

精度:毫秒,与GetTickCount()相当。使用需要包含windows.h,并加入Winmm.lib(虽然查到资料说需要包含mmsystem.h,不过经验证,可以不用包含)。测试代码如下:

#include #include//GetTickCount//#include

using namespacestd;intmain()

{

DWORD start= timeGetTime();// Sleep(1000);

DWORD end= timeGetTime();// cout << timeGetTime() <

}

5、MFC中,CTime::GetCurrentTime() 精确到秒,不列出测试代码。

关于定时器什么的,目前用到地方不多,就不总结了

参考网址:

1、http://blog.csdn.net/wallaceli1981/archive/2009/10/24/4723218.aspx

2、http://wenku.baidu.com/view/beb3c9eef8c75fbfc77db2b5.html

EXCEL计算时间差

两个单元格直接相减。

如A1 ,A2两个单元格中是时间,把它们相减即得时间差,不过EXCEL默认是按天来计的,要转换成小时或分钟等就要乘相应的数.如

相差天数的

=A1-A2

相差小时数的

=(A1-A2)*24

相差分钟的

=(A1-A2)*24*60

相差秒的,,相信你自已亦可以推导出了。。

怎计算时间差??

后面的时间加上(24小时×相差的天数),再减去前面的时间。特别注意时间的进位。

具体到楼主的问题,就是:

(12:23:34+24:00:00)-13:34:32

=(12+24):(23+00):(34+00)-13:34:32

=36:23:34-13:34:32

=(36-13):(23-34):(34-32)

=23:(-11):02

=22:(60-11):02

=22:49:02

时差是22小时49分2秒。

http://www.dengb.com/cjjc/831589.htmlwww.dengb.comtruehttp://www.dengb.com/cjjc/831589.htmlTechArticle获取时间和计算时间差的几种方法总结,时间差几种方法 转载自: http://blog.csdn.net/coder_xia/article/details/6566708 一、标准C和C++都可用 1、获取...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值