我們在進行編程的時候, 不免要測試程序的運行時間, 下面, 將介紹一些測試程序運行時間的方法.
時間的表示方法
Coordinated Universal Time:
協調世界時, 又稱為統一時間, 是世界的標准時間, 簡稱為UTC.
國際原子時(TAI)
國際原子時(TAI)是根據以下秒的定義的一種國際參照時標, 屬於國際單位制(SI)。
格林尼治標准時(GMT)
是指位於英國倫敦郊區的皇家格林威治天文台的標准時間,因為本初子午線被定義在通過那裡的經線。自1924年2月5日開始,格林威治天文台每隔一小時會向全世界發放調時信息。
當然, 除此之外還有太陽時, 世界時, 歷書時等時間標准.
標准C/C++時間測量相關函數
相關函數和類型
* processor time is measured and is returned by 'clock'.
*/
*/clock_t clock(
舉例
代碼
i+j;
}
{
clock_t start = clock();
doFunc();
clock_t finish = clock();
cout << cout <<
cout <<
}
輸出
分解時間
類型和函數介紹
tm_sec
Contains a value between 0 and 59 that indicates the number of seconds.
tm_min
Contains a value between 0 and 59 that indicates the number of hours.
tm_hour
Contains a value between 0 and 23 that indicates the number of hours since midnight.
tm_mday
Contains a value between 1 and 31 that indicates the day of the month.
tm_mon
Contains a value between 0 and 11 that indicates the number of months since January.
tm_year
Indicates the number of years since 1900.
tm_wday
Contains a value between 0 and 6 that indicates the number of days since Sunday.
tm_yday
Contains a value between 0 and 365 that indicates the number of days since January 1.
tm_isdst
Indicates daylight savings time when TRUE and normal time when FALSE.
Headers
Declared in hbaapi.h. Include hbaapi.h.
*/ };
*/# ifndef # define
Parameters
timer
Pointer to stored time. The time is represented as seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC).
Return Value
Return a pointer to a structure of type tm. The fields of the returned structure
hold the evaluated value of the timer argument in UTC rather than in local time.
Each of the structure fields is of type int.
Required header
<time.h>
*/
Parameters
timer
Pointer to stored time.
Return Value
Return a pointer to the structure result, or NULL if the date passed to the function is:
Before midnight, January 1, 1970.
After 03:14:07, January 19, 2038, UTC (using _time32 and time32_t).
After 23:59:59, December 31, 3000, UTC (using _time64 and __time64_t).
Remarks
The localtime function converts a time stored as a time_t value and stores the result in a
structure of type tm. The long value timer represents the seconds elapsed since midnight (00:00:00), January 1, 1970, UTC. This value is usually obtained from the time function.
Both the 32-bit and 64-bit versions ofgmtime, mktime, mkgmtime, and localtimeall use a single tm
structure per thread for the conversion. Each call to one of these routines destroys the result of the previous call.
localtime corrects for the local time zone if the user first sets the global environment variable TZ.
When TZ is set, three other environment variables (_timezone, _daylight, and _tzname) are automatically set as well. If the TZ variable is not set, localtime attempts to use the time zone information specified in the Date/Time application in Control Panel. If this information cannot be obtained, PST8PDT, which signifies the Pacific Time Zone, is used by default. See _tzset for a description of these variables. TZ is a Microsoft extension and not part of the ANSI standard definition of localtime.
*/
day month date hours:minutes:seconds year\n\0
Mon Jun 26 12:03:53 2000
*/_CRTIMP
舉例
代碼
{
time_t ltime;
time(<ime);
cout << }
輸出
補充
當然, 還有其他測試方法, 在這裡值介紹最通用的, 值得注意的是, 這裡獲取時間是在同一個線程中進行的, 所以, 計算程序運行時間上, 會稍微多一些, 如果使用多線程
參考文檔:
百度百科—Coordinated Universal Time
- wikipedia—原子時
- wikipedia—格林尼治標准時
- 參見MSDN