Python Forum
The number of object makes code slower? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: The number of object makes code slower? (/thread-7809.html)



The number of object makes code slower? - fig0 - Jan-25-2018

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:

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?


RE: The number of object makes code slower? - Gribouillis - Jan-25-2018

I don't believe this, there must be some other fact that you don't see. Could you post a reproducible, profilable and complete minimal example of this behavior?