Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is generally faster, a yield or an append?

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:

  1. yields, or
  2. several append()s within the function then return the ensuing list?

I would be happy to know in what situations where yields would be faster than append()s or vice-versa.

like image 861
Kit Avatar asked Aug 15 '10 14:08

Kit


2 Answers

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.

like image 138
Jochen Ritzel Avatar answered Oct 17 '22 18:10

Jochen Ritzel


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):

  • The naive approach (permutations are lists, append to list, return list of lists) takes about three times the time of itertools.permutations
  • Using a generator (ie yield) reduces this by approx. 20 %
  • Using a generator and generating tuples is the fastest, about twice the time of itertools.permutations .

Take with a grain of salt! Timing and profiling was very useful:

if __name__ == '__main__':
    import cProfile
    cProfile.run("main()")
like image 26
chryss Avatar answered Oct 17 '22 17:10

chryss