There are two things I'd like to point out about this. The first is that you have found an extremely rare beast: a CPU-bound Perl program! In practical applications of Perl, nearly all programs are limited by IO (database, file, network) rather than CPU. This is why no one would write a 3D rendering tool in Perl except as a learning exercise like this one.

The second is that most of us don't actually object to micro-optimization once you've found that a particular routine is the bottleneck. What I object to is the tendency many people have to say "I haven't done any profiling or measuring of any kind to see what the problem is, but I'm going to change all of this section to use some ugly code because 'map' is 2% faster than 'foreach'", or some such.

The biggest gains always come from changing large things, like removing an unnecessary IO operation or changing your algorithm or data structure, but if you find that a particular sub is a problem then it does make sense to try and fix it. If your object access really is the thing that's killing performance here, you could also try using arrays instead of hashes for the objects. Here's an example with your accessor optimization:

package Things5; use constant CLASS => 0; use constant SELF => 0; use constant VALUE => 1; use constant THING => 0; sub new { my $object = []; $object->[THING] = 0; return bless $object, $_[CLASS]; } sub get_it { return $_[SELF]->[THING]; } sub set_it { return $_[SELF]->[THING] = $_[VALUE]; } 1;

In reply to Re: Micro optimisations can pay off, and needn't be a maintenance problem by perrin
in thread Micro optimisations can pay off, and needn't be a maintenance problem by BrowserUk

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.