[Numpy-discussion] Functions vs Methods

Robert Kern robert.kern at gmail.com
Wed Dec 28 07:38:18 EST 2011


On Wed, Dec 28, 2011 at 06:39, Jaidev Deshpande
<deshpande.jaidev at gmail.com> wrote:
> Hi
>
> It is said that function calls are expensive. Does that mean one must
> use available methods instead?

For the most part, it usually doesn't matter enough to care about.
Whether you use the methods or the functions should be dominated by
other concerns like readability and genericity. Typically, this means
that you do want the methods, but not for performance reasons.

Note that when we say "function calls are expensive", we mean that the
call overhead for functions and methods implemented in pure Python is
expensive relative to functions and methods implemented via C. By
"overhead", we mean the extra time that isn't related to the actual
computation itself, like setting up the stack frame, packing and
unpacking the arguments, etc. Extension functions/methods don't need
to do as much of this.

Really, the performance difference only matters when you have
thousands of calls in a tight loop, at which point you should be
thinking about reorganizing your algorithm anyways. This is a
micro-optimization. Micro-optimizations have their place: namely,
after you have done all of the macro-optimizations that you can.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list