跟着文檔學python(二):time.time() 與 time.clock() 的對比與總結


一,兩個函數的文檔:

1,time.time():

time. time ( )

Return the time in seconds since the epoch as a floating point number. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.

—————————————————————————————————————————————

time. time ( )

返回從紀元(1970.1.1)至今的秒數。雖然這個函數的返回值永遠為浮點數,但並不是所有系統提供的秒數都會精確到小數點以后。一般情況下這個函數的返回值不會小於它上一次被調用的返回值,除非系統時鍾在兩次調用之間發生了重置。

2,time.clock():

time. clock ( )

On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name, but in any case, this is the function to use for benchmarking Python or timing algorithms.

On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.

 
           

—————————————————————————————————————————————

time. clock ( )

在Unix 中,將當前的處理器時間以浮點數的形式返回,單位為秒。它的精確度(准確地說是“處理器時間”的精確度)取決於同名的C函數,無論如何,這個函數是python關於時間計算的標尺。

WINDOWS中,第一次調用,返回的是進程運行的實際時間。而第二次之后的調用是自第一次調用以后到現在的運行時間。(實際上是以WIN32上QueryPerformanceCounter()為基礎,它比毫秒表示更為精確)

二,用法及總結
1,在Windows中,time.clock()更精確;
2,這兩個函數不可以混合使用;

(未完待續)

參考:Python time clock()方法 | 菜鳥教程


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM