Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีได้ Timer Ticks ณ เวลาที่กำหนดใน Python

ได้ timer ticks ณ เวลาที่กำหนดด้วย clock เอกสารแนะนำใช้สำหรับ benchmarking

ตัวอย่าง

import time
t0 = time.clock()
print("Hello")
t1 = time.clock() - t0
print("Time elapsed:", t1 - t0)  # CPU seconds elapsed (floating point)

ผลลัพธ์

Time elapsed: 0.0009403145040156798

ระบบต่างกันมีความแม่นยำต่างกันตาม internal clock (ticks/วินาที) โดยทั่วไปดีกว่า 20ms clock() คืนค่าต่างกันบนแพลตฟอร์มต่างกัน:

  • Unix: คืน current processor time เป็น float วินาที ใช้ benchmarking Python หรือ timing algorithms
  • Windows: คืน wall-clock seconds นับจากครั้งแรกที่เรียก ใช้ Win32 QueryPerformanceCounter() ความละเอียดดีกว่า 1 ไมโครวินาที