I thought it would be interesting to run BrowserUk's benchmark after having fixed the two little defects.

This is the modified code:

use strict; use warnings; use Benchmark qw( cmpthese ); my @strings = qw( USERID1|2215|Jones| USERID1|1000|Jones| USERID3|1495|Dole| USERID2|2500|Francis| USERID2|1500|Francis| ); cmpthese( -1, { outside => sub { my ( $x, $y, $z ); for (@strings) { ( $x, $y, $z ) = split /\|/; } }, outside2 => sub { my ( $x, $y, $z ); for (@strings) { ( $x, $y, $z ) = split /\|/, $_, 3; } }, inside => sub { for (@strings) { my ( $x, $y, $z ) = split /\|/; } }, inside2 => sub { for (@strings) { my ( $x, $y, $z ) = split /\|/, $_, 3; } }, } );
And the benchmark results:
$ perl bench_inside_outside.pl Rate outside outside2 inside inside2 outside 90269/s -- -20% -40% -51% outside2 113390/s 26% -- -25% -39% inside 151060/s 67% 33% -- -19% inside2 185735/s 106% 64% 23% --
So, (hoping the code is now correct), the results are now consistently showing (1) the quite strong advantage of declaring the variables inside the loop compared to doing before entering the loop (these results are well in line with AnonMonk's reported results), and (2) that choroba's idea to specify a limit also bring a measurable improvement (much less strong than the inside/outside declaration, but I would tend to think that a difference of about 25% is significant, and no longer noise).

That second point is interesting, because I have experienced in the past that specifying a limit brings an improvement when the string being split would yield (without limit) more fields than the limit, presumably because Perl is able to stop processing the string as soon as the limit is reached, but I would have thought that this advantage would to a large extent vanish when the limit is the same as the number of potential fields in the string being split. Good to know. Thank you choroba for this comment.


In reply to Re^6: Best way to store/sum multiple-field records? (carte blanche) by Laurent_R
in thread Best way to store/sum multiple-field records? by bobdabuilda

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.