c语言时间算法如何以毫秒显示,C语言时间的方法.doc
C語(yǔ)言時(shí)間的方法.doc
C語(yǔ)言獲取系統(tǒng)時(shí)間的幾種方式
—源于網(wǎng)絡(luò)
C語(yǔ)言中如何獲取時(shí)間?精度如何?
1 使用time_t time( time_t * timer ) 精確到秒
2 使用clock_t clock() 得到的是CPU時(shí)間精確到1/CLOCKS_PER_SEC秒
3 計(jì)算時(shí)間差使用double difftime( time_t timer1, time_t timer0 )
4 使用DWORD GetTickCount() 精確到毫秒
5 如果使用MFC的CTime類,可以用CTime::GetCurrentTime() 精確到秒
6 要獲取高精度時(shí)間,可以使用
BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)
獲取系統(tǒng)的計(jì)數(shù)器的頻率
BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
獲取計(jì)數(shù)器的值
然后用兩次計(jì)數(shù)器的差除以Frequency就得到時(shí)間。
7 Multimedia Timer Functions
The following functions are used with multimedia timers.
timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime
//*********************************************************************
//用標(biāo)準(zhǔn)C實(shí)現(xiàn)獲取當(dāng)前系統(tǒng)時(shí)間的函數(shù)
?
一.time()函數(shù)
???? time(&rawtime)函數(shù)獲取當(dāng)前時(shí)間距1970年1月1日的秒數(shù),以秒計(jì)數(shù)單位,存于rawtime 中。
#include "time.h"
void main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "\007The current date/time is: %s", asctime (timeinfo) );
exit(0);
}
=================
#include -- 必須的時(shí)間函數(shù)頭文件time.h
time_t -- 時(shí)間類型(time.h 定義是typedef long time_t; 追根溯源,time_t是long)
struct tm -- 時(shí)間結(jié)構(gòu),time.h 定義如下:
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
time ( &rawtime ); -- 獲取時(shí)間,以秒計(jì),從1970年1月1日起算,存于rawtime
localtime ( &rawtime ); -- 轉(zhuǎn)為當(dāng)?shù)貢r(shí)間,tm 時(shí)間結(jié)構(gòu)
asctime ( )-- 轉(zhuǎn)為標(biāo)準(zhǔn)ASCII時(shí)間格式:
星期 月 日 時(shí):分:秒 年
?
-----------------------------------------------------------------------------
二.clock()函數(shù), clock()函數(shù)返回程序開(kāi)始執(zhí)行起處理器所消耗的時(shí)間(毫秒級(jí)),然后除以CLOCKS_PER_SEC,就可以換成“秒”,標(biāo)準(zhǔn)c函數(shù)。
clock_t clock ( void );
#include
clock_t t = clock();
long sec = t / CLOCKS_PER_SEC;
他是記錄時(shí)鐘周期的,實(shí)現(xiàn)看來(lái)不會(huì)很精確,需要試驗(yàn)驗(yàn)證。
---------------------------------------------------------------------------
三.gettime(&t); 據(jù)說(shuō)tc2.0的time結(jié)構(gòu)含有毫秒信息
#include
#include
int main(void)
{
struct time t;
gettime(&t);
printf("The current time is: -:d:d.d\n",
t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund);
return 0;
}
time 是一個(gè)結(jié)構(gòu)體,, 其中成員函數(shù) ti_hund 是毫秒…
?說(shuō)明:這個(gè)在vc++6.0不行的。
-------------------
總結(jié)
以上是生活随笔為你收集整理的c语言时间算法如何以毫秒显示,C语言时间的方法.doc的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: c语言给bmp图片加滤镜,关于BMP位图
- 下一篇: android动态化ui框架,动态化高性