Omer Dagan
Omer Dagan

Reputation: 15996

python performance- function vs generator functions

I want to know which one is better in performance terms: a "regular" python function with state, or a generator. Unlike similar questions, I'm using the most simplified function to isolate the problem:

Regular function:

 >>> def counter_reg():
         if not hasattr(count_regular,"c"):
             count_regular.c = -1
         count_regular.c +=1
         return count_regular.c

Generator functions:

>>> def counter_gen():
    c = 0
    while True:
        yield c
        c += 1

>>> counter = counter_gen()
>>> counter = counter.next

In both cases, calling counter() and counter_reg() will produce the same output.

Which one is better in terms of performance? Thanks,

Upvotes: 2

Views: 1339

Answers (1)

unutbu
unutbu

Reputation: 880389

Here is an example of how you can benchmark Python functions using the timeit module:

test.py:

import itertools as IT

def count_regular():
     if not hasattr(count_regular,"c"):
         count_regular.c = -1
     count_regular.c +=1
     return count_regular.c

def counter_gen():
    c = 0
    while True:
        yield c
        c += 1

def using_count_regular(N):
    return [count_regular() for i in range(N)]

def using_counter_gen(N):
    counter = counter_gen()
    return [next(counter) for i in range(N)]    

def using_itertools(N):
    count = IT.count()
    return [next(count) for i in range(N)]    

Run python like this to time the functions:

% python -mtimeit -s'import test as t' 't.using_count_regular(1000)'
1000 loops, best of 3: 336 usec per loop
% python -mtimeit -s'import test as t' 't.using_counter_gen(1000)'
10000 loops, best of 3: 172 usec per loop
% python -mtimeit -s'import test as t' 't.using_itertools(1000)'
10000 loops, best of 3: 105 usec per loop

For a more thorough benchmarking, try different values of N, though in this case I don't think it is going to matter.

So as you would expect, using itertools.count is faster than either count_regular or counter_gen.

Upvotes: 5

Related Questions