Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Update: Ignore this post (mistakes pointed out by shmem and ikegami below.

Perhaps I'm doing something wrong, but my benchmarking results are radically different from shmem's.

For a start, with the same data (programme run several times), I get something like this:

Rate splicing radiant splicing 2371/s -- -19% radiant 2936/s 24% --

This is not the first time that I have got very different benchmarking results than other Monks on this forum, but this time the difference is particularly egregious.

In case you're wondering:

C:\Perl\progs>perl -v This is perl, v5.8.8 built for MSWin32-x86-multi-thread <snip> Binary build 817 provided by ActiveState

And the bigger the original array gets (and the greater the number of elements to insert), the more radiantmatrix's code appears to outperform splice:

C:\Perl\progs> 6000 7000 10000 Array size: 10000 Inserting: 6000 .. 7000 Rate splicing radiant splicing 28.1/s -- -86% radiant 198/s 603% --
C:\Perl\progs> 60000 61000 100000 Array size: 100000 Inserting: 60000 .. 61000 Rate splicing radiant splicing 2.87/s -- -93% radiant 42.7/s 1388% --

Perhaps I've got something very very wrong, but my findings seem to be borne out by this extract from Mastering algorithms with Perl, Chapter 3:

...splicing elements into or out of the middle of a large array can be very expensive.

Here's my benchmarking code, demolish it at will:

use strict; use warnings; use Benchmark 'cmpthese'; my ( $START, $END ) = @ARGV >= 2 ? @ARGV : ( 5000, 6000 ); my $SIZE = $ARGV[2] ? $ARGV[2] : 10000; die "Bad input!\n" if $START > $END or $END > $SIZE; my $TIMES = int 10_000_000 / $SIZE / 2; print "Array size: $SIZE\n"; print "Inserting: $START .. $END\n"; sub insert1 { my ( $ra, $index, $elem ) = @_; @$ra = @$ra[0 ..$ index-1], $elem, @$ra[$index .. @$ra-1]; } sub insert2 { my ( $ra, $index, $elem ) = @_; splice( @$ra, $index, 0, $elem ); } cmpthese ( $TIMES, { radiant => sub { my @ary = 1 .. $SIZE; insert1( \@ary, 1, $_ ) for $START .. $END }, splicing => sub { my @ary = 1 .. $SIZE; insert2( \@ary, 1 ,$_ ) for $START .. $END }, } );

In reply to Re^2: Linked lists as arrays: inserting values by Not_a_Number
in thread Linked lists as arrays: inserting values by radiantmatrix

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (7)
    As of 2018-05-22 13:33 GMT
    Find Nodes?
      Voting Booth?