#!/usr/bin/perl -w -- use strict; use Benchmark 'cmpthese'; cmpthese(-3,{ 'perloop' => sub { my @array; for(0..49) { $array[$_]=50; } return(); }, 'c-loop' => sub { my @array; for(my $i=0; $i<50; $i++) { $array[$i]=50; } return(); }, 'rangeslice' => sub { my @array; @array[0..49] = (50) x 50; return(); }, 'literalslice' => sub { my @array; @array[ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20, 21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37, 38,39,40,41,42,43,44,45,46,47,48,49 ] = ( 50,50,50,50,50,50,50,50,50,50,50, 50,50,50,50,50,50,50,50,50,50,50, 50,50,50,50,50,50,50,50,50,50,50, 50,50,50,50,50,50,50,50,50,50,50, 50,50,50,50,50,50 ); return(); }, 'literalassign' => sub { my @array = ( 50,50,50,50,50,50,50,50,50,50,50, 50,50,50,50,50,50,50,50,50,50,50, 50,50,50,50,50,50,50,50,50,50,50, 50,50,50,50,50,50,50,50,50,50,50, 50,50,50,50,50,50 ); return(); }, 'repeatassign' => sub { my @array = (50) x 50; return(); }, }); __END__ Benchmark: running c-loop, literalassign, literalslice, perloop, range +slice, repeatassign, each for at least 3 CPU seconds... c-loop: 3 wallclock secs ( 3.39 usr + 0.00 sys = 3.39 CPU) @ + 31973.75/s (n=108391) literalassign: 4 wallclock secs ( 3.09 usr + 0.00 sys = 3.09 CPU) @ + 122232.14/s (n=378064) literalslice: 4 wallclock secs ( 3.00 usr + 0.00 sys = 3.00 CPU) @ + 92383.00/s (n=277149) perloop: 4 wallclock secs ( 3.00 usr + 0.00 sys = 3.00 CPU) @ + 40371.54/s (n=121155) rangeslice: 3 wallclock secs ( 3.02 usr + 0.00 sys = 3.02 CPU) @ + 65742.71/s (n=198280) repeatassign: 4 wallclock secs ( 3.19 usr + 0.00 sys = 3.19 CPU) @ + 129130.18/s (n=411667) Rate c-loop perloop rangeslice literalslice literala +ssign repeatassign c-loop 31974/s -- -21% -51% -65% + -74% -75% perloop 40372/s 26% -- -39% -56% + -67% -69% rangeslice 65743/s 106% 63% -- -29% + -46% -49% literalslice 92383/s 189% 129% 41% -- + -24% -28% literalassign 122232/s 282% 203% 86% 32% + -- -5% repeatassign 129130/s 304% 220% 96% 40% + 6% --

In reply to Re: Re: Re: Array How to/Performance: mass initialization or update using array slice by Anonymous Monk
in thread Array How to/Performance: mass initialization or update using array slice 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.