Để lấy timer ticks tại một thời điểm, dùng time.clock(). Tài liệu Python khuyên dùng hàm này cho mục đích benchmark (đo hiệu năng).
Ví dụ
import time
t0 = time.clock()
print("Hello")
t1 = time.clock() - t0
print("Time elapsed:", t1) # CPU seconds elapsed (floating point)
Kết quả
Time elapsed: 0.0009403145040156798
Lưu ý quan trọng: time.clock() đã bị deprecated từ Python 3.3 và xóa bỏ từ Python 3.8.
- Trên Unix: trả về processor time (CPU time) tính bằng giây.
- Trên Windows: trả về wall-clock seconds kể từ lần gọi đầu tiên, dựa trên
QueryPerformanceCounter(), độ phân giải thường tốt hơn 1 microsecond.
Thay thế hiện đại: Dùng time.perf_counter() (cho benchmark độ chính xác cao) hoặc time.process_time() (cho CPU time).