The following code strives to test only chop, chomp, and a noop for the same number of iterations, with the same starting data on each iteration. As far as is practical it attempts to maintain simplicity and to establish a control group (mynada) that performs as similar an algorithm as possible to the two other alternatives. Observe this code and the results:

use strict; use warnings; use Benchmark qw(cmpthese); my @orig = ("\n") x 100000; sub mychomp { my $data = shift; my @copy = @$data; chomp for @copy; } sub mychop { my $data = shift; my @copy = @$data; chop for @copy; } sub mynada { my $data = shift; my @copy = @$data; 1 for @copy; } cmpthese -10, { chomp => sub {mychomp(\@orig)}, chop => sub {mychop(\@orig)}, nada => sub {mynada(\@orig)}, };

This produces (on my system):

Rate chomp chop nada chomp 87.8/s -- -4% -20% chop 91.6/s 4% -- -16% nada 109/s 25% 20% --

This shows me that the noop is about 20%-25% faster than the other two alternatives, and that the other two are only 4% apart, which is really within the margin for error.

The fact that you are getting more dramatically different results might indicate that the experiment is not as pristine as it should be, and that you are comparing non-equivalent alternatives.


Dave


In reply to Re: Unexplained benchmark when using chop vs. chomp (or neither) by davido
in thread Unexplained benchmark when using chop vs. chomp (or neither) by Cristoforo

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.