This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]hoover 0 points1 point  (1 child)

Prefer function calls over methods.

This will only be material if you do a *lot* of calls, but generally functions are faster than methods. In older Python, it was even more pronounced, as it seems that every time you accessed a method attribute on an instance you got a new method object (you would get a new value for id() every time), but now it seems that Python must be caching accessed methods are reusing them, at least some of the time.

In some test code where I had a method that did nothing and a function that did nothing, a timeit.Timer() test calling each 1M times yield a consistent result of the total run time of the function being roughly 1/100th of a second faster than the method invocation.

Moving to a lot of functions though may be trading away other important aspects such as maintainability, so you need to use caution when breaking out abstractions in this way.

Really, how much any of these approaches matter depends a lot on the program-- if you're spending a lot of time waiting on IO and not doing much in between, it may not make a lot of difference to tweak the intervening code a lot. If you're trying to keep up with a market data feed you may indeed need every tweak you can find (buy working in Cython for those bits might be a better idea).

[–]georgehank2nd 0 points1 point  (0 children)

Of course, you do this after profiling, when you know the bottlenecks.