Testing alternatives to for _ in range(n) (to execute some action n times, even if the action does not depend on the value of n) I noticed that there is another formulation of this pattern that is faster, for _ in [""] * n.
For example:
timeit('for _ in range(10^1000): pass', number=1000000)
returns 16.4 seconds;
whereas,
timeit('for _ in [""]*(10^1000): pass', number=1000000)
takes 10.7 seconds.
Why is [""] * 10^1000 so much faster than range(10^1000) in Python 3?
All testing done using Python 3.3
Aucun commentaire:
Enregistrer un commentaire