Which one’s a better way of doing list comprehension in python (in terms of computation time & cpu cycles).
In example (1) is the value f(r) evaluated in each iteration or is it evaluated once and cached ?
-
y = [x*f(r) for x in xlist] -
c = f(r)y = [x*c for x in xlist]
where
def f(r):
... some arbitrary function ...
I would probably choose the latter because the Python compiler doesn’t know if the function has side-effects so it is called for each element.