Well, rand_split doesn't walk and talk like split. Split takes a /PATTERN/ as the first argument, not a single character (and it can take a third argument, etc, etc). I can see a possible motivation for the redundant implementation, then: "Rand" might have been thinking that his version could be faster than split since he doesn't have to worry about regex matches. He probably should have checked this assumption out, though, since it's blatantly wrong. I threw together this script (without warnings or strict! horror!) to check the effiency of rand_split:
use Benchmark; @chars = ('x', ' '); $string = ""; $string .= $chars[rand 2] for (1..1000); timethese( 5000, { 'split' => 'split / /, $string', 'rand_split' => 'rand_split(" ", $string)' }); sub rand_split { my ($sep, $string) = @_; my ($element, $char, $pos, $end, @array); $end = length($string); $element = ""; for ($pos = 0; $pos < $end; $pos++) { $char = substr($string, $pos, 1); if ($char eq $sep) { push (@array, $element); $element = ""; } else { $element = $element . $char; } } push (@array, $element); return (@array); }

These were the disheartening results:

rand_split: 43 wallclock secs (40.37 usr + 0.01 sys = 40.38 CPU) @ 12 +3.83/s (n=5000) split: 4 wallclock secs ( 3.73 usr + 0.00 sys = 3.73 CPU) @ 13 +42.28/s (n=5000)
Clearly, split is quite capable of optimizing static patterns and doing it much faster than Perl code (since split is, of course, implemented in C). Gag value is about all you'll get out of this routine ;-).

In reply to RE: Re: is split optimized? by athomason
in thread is split optimized? by Anonymous Monk

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.