Should I Cache Range Results If I Reuse Them?
Solution 1:
In python 3, range
is a generator. It means that it will yield all the numbers of the sequence. It's simple additions.
You could cache that in a list
: cache = list(range(10))
but that would allocate some memory and would need to iterate on it: not productive!
BTW: the first example does not cache the result, you just copy the generator function. You may save a few microseconds of parsing time (because the function is already parsed, not really worth it)
So, no, it is not useful to cache the result of range
in python 3 (in python 2, it would be useful, yes, since it creates an actual list
).
Solution 2:
It won't make a noticeable difference, the majority of the time is spent inside of the loop, not generating the range
object.
range
in Python 3 returns a sequence, which means it will generate the values on the fly, without having to store them in a list.
Post a Comment for "Should I Cache Range Results If I Reuse Them?"