Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Even know <em>Premature optimisation is the root of all evil</em></p> <pre><code>{ local $\ = "\n"; print foreach @numbers; } </code></pre> <p>but some expectations can be wrong. Test is little bit weird because output can make some weird side-effects and order can be important.</p> <pre><code>#!/usr/bin/env perl use strict; use warnings; use Benchmark qw(:all :hireswallclock); use constant Numbers =&gt; 10000; my @numbers = (1 .. Numbers); sub no_out (&amp;) { local *STDOUT; open STDOUT, '&gt;', '/dev/null'; my $result = shift()-&gt;(); close STDOUT; return $result; }; my %tests = ( loop1 =&gt; sub { foreach my $current (@numbers) { print "$current\n"; } }, loop2 =&gt; sub { foreach (@numbers) { print "$_\n"; } }, loop3 =&gt; sub { local $\ = "\n"; print foreach @numbers; } ); sub permutations { return [ map { my $a = $_; my @f = grep {$a ne $_} @_; map { [$a, @$_] } @{ permutations( @f ) } } @_ ] if @_; return [[]]; } foreach my $p ( @{ permutations( keys %tests ) } ) { my $result = { map { $_ =&gt; no_out { sleep 1; countit( 2, $tests{$_} ) } } @$p }; cmpthese($result); } </code></pre> <p>One can expect that loop2 should be faster than loop1</p> <pre><code> Rate loop2 loop1 loop3 loop2 322/s -- -2% -34% loop1 328/s 2% -- -33% loop3 486/s 51% 48% -- Rate loop2 loop1 loop3 loop2 322/s -- -0% -34% loop1 323/s 0% -- -34% loop3 486/s 51% 50% -- Rate loop2 loop1 loop3 loop2 323/s -- -0% -33% loop1 324/s 0% -- -33% loop3 484/s 50% 49% -- Rate loop2 loop1 loop3 loop2 317/s -- -3% -35% loop1 328/s 3% -- -33% loop3 488/s 54% 49% -- Rate loop2 loop1 loop3 loop2 323/s -- -2% -34% loop1 329/s 2% -- -33% loop3 489/s 51% 49% -- Rate loop2 loop1 loop3 loop2 325/s -- -1% -33% loop1 329/s 1% -- -32% loop3 488/s 50% 48% -- </code></pre> <p>Sometimes I observed consistently <code>loop1</code> about 15%-20% faster than <code>loop2</code> but I can't determine why.</p> <p>I was observed generated byte-code for loop1 and loop2 and there is difference only one when creating <code>my</code> variable. This variable interior is not allocated and also not copied thus this operation is very cheap. Difference comes I think only from <code>"$_\n"</code> construct which is not cheap. These loops should be very similar</p> <pre><code>for (@numbers) { ... } for my $a (@numbers) { ... } </code></pre> <p>but this loop is more expensive</p> <pre><code>for (@numbers) { my $a = $_; ... } </code></pre> <p>and also </p> <pre><code>print "$a\n"; </code></pre> <p>is more expensive than </p> <pre><code>print $a, "\n"; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload