> > Function calls in Perl are dead slow compared
> > to compiled languages.
>
> Ok, so why is this?
>
Implementation and little optimization.
Perl has a complex function calling mechanism, as
it flattens argument lists by pushing elements onto
the stack, records argument list length - the
number of arguments are variable in length.
Unlike many compiled languages, Perl does not
inline short/frequently called functions - with
the result that speed suffers.
An example:
foreach (1..1_000_000) {
nop();
}
sub nop {}
gives:
real 0m34.790s
user 0m34.750s
sys 0m0.050s
on a DX2/66, rather impressive for a loop that does nothing. Do it again without the
sub call,
and it takes:
real 0m7.287s
user 0m7.240s
sys 0m0.050s
so it's 34-7 = 27 seconds longer in a tight loop. Since I've looped a million times,
and CPU
speeds are in Mhz, that means I can take 66*27 = ~1782 machine instructions (probably
about
%10-%30 less). That is still an awful lot, an assembly version of one iteration looks
a little
like:
JSR .NULL # Jump to subroutine .NULL
END # Mythical end function
..NULL # Label .NULL
RET # Return from subroutine
which is a mimimum of four instructions (in reality, probably no more than ~30 once
stack frame is
saved).
My figures are only ball-park, don't expect them to match exactly what you'd find in a
textbook...
same goes for the Assembly (which isn't anything in particular ;-).
> will this get 'fixed' in p6?
Probably an improvement.
Jonathan Paton
__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]