python3.8 执行报错AttributeError: module ‘time‘ has no attribute ‘clock‘的解决办法

问题:

python3.8 在调用time.clock()时,导入没有问题,但在执行时报错如下图:

python3.8 执行报错AttributeError: module ‘time‘ has no attribute ‘clock‘的解决办法_第1张图片

原因分析:

我们通过查看time.clock()方法源码可以发现,python版本小于3.8,才支持time.clock()方法,3.8版本开始不支持time.clock()方法了,但调用时依然包含该方法,源码如下:
python3.8 执行报错AttributeError: module ‘time‘ has no attribute ‘clock‘的解决办法_第2张图片

解决方案:

当python版本大于等于3.8时,我们可以使用time.perf_counter()方法来替换time.clock()方法,统计代码运行时间

你可能感兴趣的:(python)