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.
RIP an inspiration; A true Folk's Guy

In reply to Re^3: Some code optimization by BrowserUk
in thread Some code optimization by roibrodo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.