Combining your method with index instead of regex and it goes quicker still.

Updated: Right conclusions, wrong evidence. Ignore this the tests are bad.

#! 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, @c, @d ); 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 ], Abi_regex => q[ @c = map{ substr $_ => 1 } sort map{ /:/ ? "0$_" : "1$_" } @strings ], Abi_index => q[ @c = map{ substr $_ => 1 } sort map{ 1+index($_, ':') ? "0$_" : "1$_" } @strings ], }); print 'Okay' if "@a" eq "@b" or "@b" ne "@c" or "@c" ne "@d"; __END__ P:\test>test3 Rate regex index Abi_regex Abi_index regex 13.9/s -- -14% -21% -27% index 16.3/s 17% -- -7% -15% Abi_regex 17.6/s 26% 8% -- -9% Abi_index 19.2/s 38% 18% 9% -- Okay

Updated: I extended the tests without testing the tests! D'oh.

#! 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, @c, @d ); 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 ], Abi_regex => q[ @c = map{ substr $_ => 1 } sort map{ /:/ ? "1$_" : "0$_" } @strings ], Abi_index => q[ @d = map{ substr $_ => 1 } sort map{ 1+index($_, ':') ? "1$_" : "0$_" } @strings ], }); print 'Okay' if "@a' eq '@b" and "@b" eq "@c" and "@c" eq "@d"; __END__ P:\test>test3 Rate regex index Abi_regex Abi_index regex 14.5/s -- -11% -16% -24% index 16.2/s 12% -- -6% -15% Abi_regex 17.1/s 19% 6% -- -10% Abi_index 18.9/s 31% 17% 11% -- Okay

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


In reply to Re: 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.