Hmm. I originally used a debug session & timethese(), did a little mental arithmetic, that jelled with what Anonymonk said, so I went with it.

Now I've put it into a proper script, used cmpthese() to do the arithmetic, and I can't reproduce my original results. In fact, I seem to consistantly get the index version coming out quicker, sometimes markedly so??

Maybe I did the arithmetic wrong, or there is a flaw in my benchmark? I used random strings with (or without) randomly positioned ':' to try cover the failing conditions as well as a good spread of the passing ones. The spread of variation in timings seem to indicate that some combinations cause the regex engine to take much longer than others. I've not managed to get the difference less than 9% in favour of index, and on one occasion, it went as high as 28%. Further investigation is called for:)

#! perl -slw use strict; use Benchmark qw[ cmpthese ]; sub rndStr{ join'', map{ $_[ rand scalar @_ ] } 0 .. shift } our @strings = map{ rndStr 8, ':', 'a' .. 'z' } 1 .. 1000; our( @a, @b ); cmpthese( -3, { regex => q[ our @a = sort{ ( $a =~ /:/ <=> $b =~ /:/ ) || $a cmp $b } @strings ], index => q[ our @b = sort{ ( ( index($a,':') >= 0 ) <=> ( index($b,':') >= 0) ) || $a cmp $b; } @strings ], }); print 'Okay' if "@a" eq "@b"; __END__ P:\test>test3 Rate regex index regex 14.6/s -- -9% index 16.1/s 10% -- Okay P:\test>test3 Rate regex index regex 13.7/s -- -15% index 16.2/s 18% -- Okay P:\test>test3 Rate regex index regex 14.5/s -- -11% index 16.3/s 13% -- Okay P:\test>test3 Rate regex index regex 13.1/s -- -19% index 16.2/s 23% -- Okay

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!
Wanted!


In reply to Re+: Genesis of a sort routine by BrowserUk
in thread Genesis of a sort routine by greenFox

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.