in reply to Performance issues with subroutines/methods

With your benchmark, you are counting the overhead of calling subs in various ways. If you access a memory CELL directly (in machine language), that will be significantly faster than calling a function which computes the location of this CELL, and then accesses it. Same here. Subroutines have essentially the same performance penalty like method calls, since method calls are just subs.

Calling subroutines means, in perl, not only that some code segment in memory is jumped to, but the build up of copious data structures by which the perl engine can keep track of context, and the tear-down of those stackframes after exiting. There is some optimization going on under the hood, as with subs declared with the NULL prototype returning a scalar and not relying on dynamic data or calling other subs, in which case their result will be inlined. But apart from that, calling subs just involves the overhead of, precisely - calling subs :-)

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
  • Comment on Re: Performance issues with subroutines/methods