Surprisingly, using itertools.repeat(value).next
is actually about 30% two to three times faster in Python 2 and Python 3 (with an obvious change to __next__
).
This is not much, but also no reason to spend it.
PS: I would say that this shows that lambda
can be improved (I see no logical reason to have a related method faster than a closure), but lambda
not very popular in the Python community. Strike>
The reason for this is that itertools
primitives itertools
implemented in C, and lambda executes Python bytecode. Just returning the captured value is very fast, but nonetheless, it is still bytecode and requires a lot of tweaking / breaking like any Python function call.
source share