Python 表演时间减去

Python performance time in minus

我曾经使用相同的方法,但我得到的是以秒为单位的性能时间(例如,0.0003)。这次我不知道为什么我得到的时间是负数。我曾经使用相同的设备。我用Linux Ubuntu 18。这是一个例子:

import time 
import datetime

start = time.perf_counter() 
print("something")
duration = time.process_time() - start
print("performance time: ", duration, "sec.")

输出:

performance time:  -30559.147778458 sec.

有什么问题吗?如何获得性能。时间为正值?

time.perf_counter()time.process_time() 不 return 相同的数据...你不能从另一个中减去一个,除了垃圾之外别无所求。

time.perf_counter()

Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.

time.process_time()

Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process. It does not include time elapsed during sleep. It is process-wide by definition. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.


选择 time.perf_counter()time.process_time() - 但不要混淆 returned 值。

我怀疑你会想要使用 time.process_time()