Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Why are objects slower than procedural subs/data structs?

( #326269=categorized question: print w/ replies, xml ) Need Help??
Contributed by Sol-Invictus on Feb 03, 2004 at 17:50 UTC
Q&A  > object-oriented programming


what is it that makes objects 20-50% more expensive to use?

Answer: Why are objects slower than procedural subs/data structs?
contributed by Fletch

Method dispatch overhead. When you say foo( $a ) whatever foo in the current scope is called dirrectly. When you say $obj->bar( $b ), then perl has to check what $obj is (a scalar holding a blessed ref? a class name), then try and call Whatever::bar, and if that doesn't work try SuperWhatever::bar . . .

Granted there's some caching done, but it's more than just a simple subroutine call.

contributed by pajout

Object Methods Calls Versus Function Calls contains nicely described benchmarks. The simple reason is that it very depends on CPU-cost of subroutine bodies.


  • Please read these before you post! —
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (15)
    As of 2014-07-10 22:23 GMT
    Find Nodes?
      Voting Booth?

      When choosing user names for websites, I prefer to use:

      Results (217 votes), past polls