Skip to content

Instantly share code, notes, and snippets.

@autf
Created September 16, 2022 09:35
Show Gist options
  • Save autf/79689148aca0a324b180779d55f62eac to your computer and use it in GitHub Desktop.
Save autf/79689148aca0a324b180779d55f62eac to your computer and use it in GitHub Desktop.
from timeit import Timer
cases = list(ln.split('#') for ln in
"""array('i', [0]) * n #arr * n
array('i', [0]*n) #arr(lst*n)
array('i', range(n)) #arr(range)
array('i', (0 for _ in range(n))) #arr(0 for range)
""".splitlines())
for i in range(8):
n = 10 ** i
print(f'\n{n = } = 1e{i}')
for stmt, tag in cases:
k, t = Timer(stmt,
setup='from array import array',
globals={'n': n}
).autorange()
print(f'{t/k*1000: 10.4f} ms : {tag}')