测试代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import time

def test(func, times=1e6):
    start = time.time()
    for _ in range(int(times)):
        func()
    return time.time() - start

print(f'Time using list(): {test(lambda: list())}')
print(f'Time using []    : {test(lambda: [])}')
# Time using list(): 0.13603496551513672
# Time using []    : 0.07425308227539062

print(f'Time using dict(): {test(lambda: dict())}')
print(f'Time using {{}}    : {test(lambda: {})}')
# Time using dict(): 0.170487642288208
# Time using {}    : 0.08045601844787598