I was fiddling around with a little function to create tuple arrays filled with 0 and went from
tuple(tuple(0 for _ in range(x)) for _ in range(y))
to
((0,) * x,) * y
and it tremendously improved performance and I was wondering why it improved so much.
My test code:
import timeit
statements = (
'((0,) * x,) * y',
'tuple(tuple(0 for _ in range(x)) for _ in range(y))',
)
for i in (3, 5, 7):
setup = 'x = {}; y = {}'.format(i, i)
print()
for statement in statements:
print(sum(timeit.repeat(statement, setup))/5)
results:
0.13270888
2.5220104
0.13536394000000024
4.04865446
0.1402734800000019
6.28384762
0.2838335400000034
37.955879499999995