user13522091
user13522091

Reputation:

Do uncalled functions cost performance?

I want to know if I for example wrote 100 function in a class or even without a class and only used one function in each time I call the class, Does these too many uncalled and unused functions influence the performance or count for something negative?

Upvotes: 5

Views: 312

Answers (1)

Roy2012
Roy2012

Reputation: 12493

The answer is practically no. Chunks of code that aren't executed don't influence the performance of the program. This is true for most / all programming languages - not just Python.

That being said, there are some scenarios where this is not accurate:

  • If your program is very large, it may take a while to load. Once it loads, the execution time with or without the redundant code is the same, but there's a difference in load time.
  • More code may impact memory organization, which in turn may impact the OS' ability to cache stuff in an effective manner. It's an indirect impact, and unless you know exactly what you're doing it's mostly theoretical.
  • If you have a very large number of methods in a class, looking up a given method in a class' dictionary may take longer. The average cost of getting an item from a dict is O(1), but worst case can be O(N). You'll have to do a lot of optimization to (maybe) get to a point where you care about this.
  • There might be some other obscure scenarios in which code size impacts performance - but again, it's more theory than practice.

Upvotes: 3

Related Questions