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


Messages In This Thread
The number of object makes code slower? - by fig0 - Jan-25-2018, 09:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing the code line number arbiel 6 1,829 Jun-30-2024, 08:01 AM
Last Post: arbiel
  What a difference print() makes Mark17 2 1,507 Oct-20-2023, 10:24 PM
Last Post: DeaD_EyE
  Tkinter + Multiprocessing startmap makes UI Freeze sunny9495 4 2,700 Nov-13-2022, 12:49 PM
Last Post: sunny9495
  help me simple code result min and max number abrahimusmaximus 2 1,859 Nov-12-2022, 07:52 AM
Last Post: buran
  Upgrade makes Code Error kucingkembar 6 5,367 Jul-28-2022, 06:44 PM
Last Post: kucingkembar
  This is an Object - Code interpretation JoeDainton123 2 2,674 Jun-16-2021, 08:11 PM
Last Post: snippsat
  Even number code syntax error MrCeez 1 3,072 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 7,883 May-02-2021, 03:45 PM
Last Post: Anldra12
  Python Program running a lot slower after change to Ubuntu hubenhau 1 3,880 Mar-02-2021, 05:01 PM
Last Post: Serafim
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 3,983 Jun-18-2020, 04:59 PM
Last Post: QTPi

Forum Jump:

User Panel Messages

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