计算程序运行时间:计算或者不计算sleep()的两种情况perf_counter()和process_time()

【小白从小学Python、C、Java】
【计算机等级考试+500强双证书】
【Python-数据分析】
计算程序运行时间:
计算或者不计算sleep()的两种情况
perf_counter()和process_time()

[太阳]选择题

对下面描述错误的选项为?
import time
print('使用perf_counter计算程序运行时间')
print('记录开始时间T1')
T1 = time.perf_counter()
print('T1:', T1)
time.sleep(1)
for i in range(1000*1000):
pass
print('记录结束时间T2')
T2 =time.perf_counter()
print('T2:', T2)
print('时间T1和T2做差得到运行时间:')
print('程序运行时间(毫秒):',(T2 - T1)*1000)

import time
print('使用process_time计算程序运行时间')
print('记录开始时间T1')
T1 = time.process_time()
print('T1:', T1)
time.sleep(1)
for i in range(1000*1000):
pass
print('记录结束时间T2')
T2 =time.process_time()
print('T2:', T2)
print('时间T1和T2做差得到运行时间:')
print('程序运行时间(毫秒):',(T2 - T1)*1000)

A选项:代码中两次计算得到的程序运行时间相差约1秒

B选项:代码中两次计算得到的程序运行时间基本相同

C选项:perf_counter()输出时间值的起点是未定义的

D选项:process_time()输出时间值的起点是未定义的

正确答案是:B

计算程序运行时间:计算或者不计算sleep()的两种情况perf_counter()和process_time()_第1张图片

计算程序运行时间:计算或者不计算sleep()的两种情况perf_counter()和process_time()_第2张图片 

 

[太阳]温馨期待
期待大家提出宝贵建议,互相交流,收获更大,助教:dmx
#微博公开课# [握手] #IT研究所#

欢迎大家转发,一起传播知识和正能量,帮助到更多人。期待大家提出宝贵改进建议,互相交流,收获更大。辛苦大家转发时注明出处(也是咱们公益编程交流群的入口网址),刘经纬老师共享知识相关文件下载地址为:https://liujingwei.cn

你可能感兴趣的:(python)