Jan-25-2018, 09:00 PM
I'm profilling my script to find its speed bottlenecks, but there is an anomaly that is messing with my understanding of how python interpreter works.
I have a class definition in my code that looks like the following:
It happens that the code get faster (in terms of cProfile percall metric of interface.play()) if I do not instantiate the ConcreteClass, ie, not having the c object.
It goes from 1.7s (with c object) to 0.04 for 100 calls of the same method in both scenarios.
Why does the c object interferes so much in the while loop?
I have a class definition in my code that looks like the following:
import foo class ConcreteClass(foo.AbstractClass): init(self, id): super().init(id)And then I use a context manager inside of a with statement to open a connection with a local server (a simulation software), instantiate the ConcreteClass and keep calling the connection interface inside of a while loop to play a timestep inside of simulation:
with foo.interface() as interface: c = ConcreteClass("foo") while True: interface.play()As one can see, I do not make any other function call inside of the while context except for the interface.play().It happens that the code get faster (in terms of cProfile percall metric of interface.play()) if I do not instantiate the ConcreteClass, ie, not having the c object.
It goes from 1.7s (with c object) to 0.04 for 100 calls of the same method in both scenarios.
Why does the c object interferes so much in the while loop?