in reply to When every microsecond counts: Parsing subroutine parameters

When every microsecond counts you factor out expensive stuff into XS code. Or don't use perl.

That said, constructing (and tearing down) a hash (which happens if you call foo( {bar => 'quux'} )) at every subroutine invocation is expensive, but calling a sub in the first place is probably more expensive (stack frame setup / enter / leave / unwind cycle).

--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}

Replies are listed 'Best First'.
Re^2: When every microsecond counts: Parsing subroutine parameters
by snowhare (Friar) on May 17, 2008 at 19:32 UTC
    XS has a surprising amount of overhead per call copying parameters in and out. When I was writing Lingua::Stem I was able to achieve 80% of the speed of Lingua::Stem::Snowball (which uses XS) for stemming in place and actually beat Lingua::Stem::Snowball by 30% for large straight list processing.
      All parameter passing / subroutine calls involving perl are relatively slow, and conversion to and from C types can probably make XS even slower than pure perl if used naively. The best way to use XS/C is to reduce the number of calls back and forth (IOW, move your iterations to C code, don't loop over XS calls where speed really matters). C compilers are very good at inlining and otherwise optimizing subroutine calls. Perl isn't, really - even though it's overall pretty damn fast compared to some other "scripting" languages I could mention.