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?
source
share