10 月 222020
# 列表操作方式的效率比较
import time
a = []
# t0 = time.clock() # 异常:AttributeError: module 'time' has no attribute 'clock'
# https://docs.python.org/3/whatsnew/3.8.html
# https://docs.python.org/3/library/time.html#time.perf_counter
# 该函数在python3.3废弃,在python3.8移除
t0 = time.perf_counter()
for i in range(1, 20000):
a.append(i)
print(time.perf_counter() - t0, 'seconds process time')
t0 = time.perf_counter()
b = [i for i in range(1, 20000)]
print(time.perf_counter() - t0, 'seconds process time')
C:\Users\harveymei\PycharmProjects\hellopython\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/hellopython/hello.py
0.0017573999999999992 seconds process time
0.0005343000000000014 seconds process time
Process finished with exit code 0
# 列表操作方式的效率比较
import time
a = []
# t0 = time.clock() # 异常:AttributeError: module 'time' has no attribute 'clock'
# https://docs.python.org/3/whatsnew/3.8.html
# https://docs.python.org/3/library/time.html#time.perf_counter
# 该函数在python3.3废弃,在python3.8移除
t0 = time.perf_counter() # 调用函数返回精确浮点性能计数值,两次调用返回值的差计算程序运行时间
for i in range(1, 20000):
a.append(i)
t1 = time.perf_counter()
print('T0 is', t0)
print('T1 is', t1)
print(t1 - t0, 'seconds process time')
t0 = time.perf_counter()
b = [i for i in range(1, 20000)]
print(time.perf_counter() - t0, 'seconds process time')
C:\Users\harveymei\PycharmProjects\hellopython\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/hellopython/hello.py
T0 is 0.0509396
T1 is 0.052513
0.0015733999999999956 seconds process time
0.0005889999999999992 seconds process time
Process finished with exit code 0