Python Forum
The number of object makes code slower?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The number of object makes code slower?
#1
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?
Reply
#2
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What a difference print() makes Mark17 2 517 Oct-20-2023, 10:24 PM
Last Post: DeaD_EyE
  Tkinter + Multiprocessing startmap makes UI Freeze sunny9495 4 1,322 Nov-13-2022, 12:49 PM
Last Post: sunny9495
  help me simple code result min and max number abrahimusmaximus 2 869 Nov-12-2022, 07:52 AM
Last Post: buran
  Upgrade makes Code Error kucingkembar 6 2,967 Jul-28-2022, 06:44 PM
Last Post: kucingkembar
  This is an Object - Code interpretation JoeDainton123 2 1,767 Jun-16-2021, 08:11 PM
Last Post: snippsat
  Even number code syntax error MrCeez 1 2,245 May-02-2021, 06:43 PM
Last Post: Larz60+
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 5,101 May-02-2021, 03:45 PM
Last Post: Anldra12
  Python Program running a lot slower after change to Ubuntu hubenhau 1 2,838 Mar-02-2021, 05:01 PM
Last Post: Serafim
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,679 Jun-18-2020, 04:59 PM
Last Post: QTPi
  Factorial Code is not working when the given number is very long integer Raj_Kumar 2 2,257 Mar-31-2020, 06:40 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020