I really encourage you to Benchmark instead of assuming. You will get surprises somewhat often. And assuming a simple arithmetic op is a performance killer seems silly. It took 10 million iterations to even be able to get this to benchmark without complaining it wasn't enough to be sure; and while a fifteenth of a millionth of a second is indeed three times faster than a fifth of a millionth of a second… does it actually matter on any level for the application in question?

moo@cow[483]~>perl pm-dice use strictures; use Benchmark "cmpthese"; sub d10 { my $self = shift; return rand(10); } sub d10_2 { rand(10) } sub d10_3 { 1 + int( rand(10) ) } sub d10_4 { 1 + rand(10) } cmpthese( 10_000_000, { orig => \&d10, orig_simple => \&d10_2, int_plus_one => \&d10_3, plus_one => \&d10_4, }); __END__ Rate orig int_plus_one plus_one orig_si +mple orig 4048583/s -- -19% -38% + -74% int_plus_one 5000000/s 24% -- -24% + -68% plus_one 6578947/s 62% 32% -- + -59% orig_simple 15873016/s 292% 217% 141% + --

In reply to Re^3: A small Deity AI class system by Your Mother
in thread A small Deity AI class system by holyghost

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.