I am currently in a personal learning project where I read in an XML database. I find myself writing functions that gather data and I'm not sure what would be a fast way to return them.
Which is generally faster:
yield
s, or append()
s within the function then return
the ensuing list
?I would be happy to know in what situations where yield
s would be faster than append()
s or vice-versa.
yield
has the huge advantage of being lazy and speed is usually not the best reason to use it. But if it works in your context, then there is no reason not to use it:
# yield_vs_append.py
data = range(1000)
def yielding():
def yielder():
for d in data:
yield d
return list(yielder())
def appending():
lst = []
for d in data:
lst.append(d)
return lst
This is the result:
python2.7 -m timeit -s "from yield_vs_append import yielding,appending" "yielding()"
10000 loops, best of 3: 80.1 usec per loop
python2.7 -m timeit -s "from yield_vs_append import yielding,appending" "appending()"
10000 loops, best of 3: 130 usec per loop
At least in this very simple test, yield
is faster than append.
I recently asked myself a similar question exploring ways of generating all permutations of a list (or tuple) either via appending to a list or via a generator, and found (for permutations of length 9, which take about a second or so to generate):
itertools.permutations
yield
) reduces this by approx. 20 %itertools.permutations
.Take with a grain of salt! Timing and profiling was very useful:
if __name__ == '__main__':
import cProfile
cProfile.run("main()")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With