How to make Python generators as fast as possible?

To write an event-driven simulator, I rely on simpy , which uses Python generators heavily. I am trying to understand how to make generators as fast as possible, i.e. Minimize the cost of maintaining / restoring state. I tried three alternatives

  • All state stored in class instance
  • All state is stored globally
  • All state saved locally

and got the following results with Python 3.4.3:

class_generator 20.851247710175812
global_generator 12.802394330501556
local_generator 9.067587919533253

The code can be found here .

This seems intriguing to me: saving all the state in an instance of the class means that you need to save / restore only self, while storing the entire state on a global scale should guarantee zero saving / restoring overhead.

Does anyone know why class generators and global generators are slower than local generators?

+4
source share
2 answers

The only state that the generator needs to save is a link to the stack stack, so saving and restoring the state is performed exactly at the same time, regardless of how many states are involved and where you put the data.

, , , Python : , , , "self", ( , , ).

+6

, . , 1 100 .

- , Python ( CPython, , http://www.python.com/, /usr/bin/python, , ) :

  • Python; LOAD_FAST.

  • LOAD_GLOBAL opcode. , .

  • , self.foobar LOAD_FAST self, LOAD_ATTR foobar , . , , , , . , , STORE_ATTR . , - , .

, - , . Python , .

, , 3 a, b self.c:

a = 42

class Foo(object):
    def __init__(self):
        self.c = 42

    def foo(self):
        b = 42
        yield a
        yield b
        yield self.c

print(list(Foo().foo()))   # prints [42, 42, 42]

foo:

  8           6 LOAD_GLOBAL              0 (a)
              9 YIELD_VALUE
             10 POP_TOP

  9          11 LOAD_FAST                1 (b)
             14 YIELD_VALUE
             15 POP_TOP

 10          16 LOAD_FAST                0 (self)
             19 LOAD_ATTR                1 (c)
             22 YIELD_VALUE
             23 POP_TOP

LOAD_GLOBAL LOAD_ATTR a c ; - . LOAD_FAST .

+10

All Articles