in reply to Re: Some code optimization
in thread Some code optimization

Function call overhead is relatively high in Perl.

I could be wrong, but if I understand the OP correctly, the function needs in the order of several seconds for just 50 calls.  Perl's function call overhead may be high, but it shouldn't be that high...

Replies are listed 'Best First'.
Re^3: Some code optimization
by BrowserUk (Patriarch) on Jun 17, 2010 at 17:29 UTC
    if I understand the OP correctly,

    Hence the reason for reserving the right to change my mind. I couldn't make head nor tails of this:

    I get 1 seconds per 50 iterations. Obviously better than the 28 seconds, but still significantly higher than the 9 seconds

    It also doesn't ring true that

    The function itself is quite simple - it gets 3 scalars (one of which is a field of some object), checks a couple of if's on the values and some basic math (including modulo %), and returns an array with a couple of hashes, each with two numerical fields. That's it.

    Would require 28 seconds/50 iterations.

    A tentative attempt to match the description yields :

    sub x{ my( $s1, $s2, $s3 ) = @_; $s1 *= 10 if $s1; $s2 **= 4 if $s2; $s3 *= $s2 % $s1; return [ { n1 => $s1, n2 => $s2 }, { n1 => 41 * $s2, n2 => ( $s1 * $s2 ) % $s3 } ]; };; $I = 50e3; $t = time; x( 123, 456, 789 ) for 1 .. $I; printf "$I calls took %.4f seconds\n", time()-$t;; 50000 calls took 0.3174 seconds $I = 50e5; $t = time; x( 123, 456, 789 ) for 1 .. $I; printf "$I calls took %.4f seconds\n", time()-$t;; 5000000 calls took 26.5590 seconds

    I appreciate my sub is guesswork and nothing like the real thing, but given the description, it is hard to guess what else that description is hiding that causes the sub to take a million times longer?

    Of course, it'll turn out that he's tallying the national debt. Or crowd sourcing the math to Twitter.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.