描述
方法clock()以 Unix 上以秒表示的浮點數形式返回當前處理器時間。精度取決於同名 C 函數的精度,但無論如何,這是用於對 Python 或計時算法進行基準測試的函數。
在Windows,此函數根據 Win32 函數 QueryPerformanceCounter 以浮點數形式返回自第一次調用此函數以來經過的 wall-clock 秒。
用法
以下是語法clock()方法≫
time.clock()
參數
NA
返回值
此方法以浮點數形式返回當前處理器時間,在 Unix 上以秒表示,在 Windows 中,它以浮點數形式返回自第一次調用此函數以來經過的 wall-clock 秒。
示例
下麵的例子展示了 clock() 方法的用法。
#!/usr/bin/python3
import time
def procedure():
time.sleep(2.5)
# measure process time
t0 = time.clock()
procedure()
print (time.clock() - t0, "seconds process time")
# measure wall time
t0 = time.time()
procedure()
print (time.time() - t0, "seconds wall time")
結果
當我們運行上麵的程序時,它會產生以下結果——
2.4993855364299096 seconds process time 2.5 seconds wall time
注意- 並非所有係統都可以測量真正的過程時間。在此類係統(包括 Windows)上,時鍾通常測量自程序啟動以來的牆上時間。
相關用法
- Python 3 time ctime()用法及代碼示例
- Python 3 time strftime()用法及代碼示例
- Python 3 time time()用法及代碼示例
- Python 3 time asctime()用法及代碼示例
- Python 3 time altzone()用法及代碼示例
- Python 3 time localtime()用法及代碼示例
- Python 3 time mktime()用法及代碼示例
- Python 3 time gmtime()用法及代碼示例
- Python 3 time sleep()用法及代碼示例
- Python 3 time tzset()用法及代碼示例
- Python 3 time strptime()用法及代碼示例
- Python 3 Number tan()用法及代碼示例
- Python 3 os.fstatvfs()用法及代碼示例
- Python 3 List pop()用法及代碼示例
- Python 3 os.minor()用法及代碼示例
- Python 3 dictionary cmp()用法及代碼示例
- Python 3 String isupper()用法及代碼示例
- Python 3 os.close()用法及代碼示例
- Python 3 List index()用法及代碼示例
- Python 3 String decode()用法及代碼示例
注:本文由純淨天空篩選整理自 Python 3 - time clock() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。