Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: How can I skip an element in an array whilst reading...

by tachyon (Chancellor)
on Apr 07, 2002 at 10:17 UTC ( [id://157254]=note: print w/replies, xml ) Need Help??


in reply to How can I skip an element in an array whilst reading...

This C style approach is 50% faster than the push approach posted earlier. Ugly but fast.

use Benchmark; @in = (1,2,3,4,5,6,7,8,9,0); timethese ( 100000, { 'C-style' => 'for my $i ( 0 .. $#in ) { $i % 2 ? $out2[int($i/2)] = $in[$i] : $out1[$i/2] = $i +n[$i] }', 'push' => 'for my $i ( 0 .. $#in ) { $i % 2 ? push @out2, $in[$i] : push @out1, $in[$i]; }' } ); __DATA__ Benchmark: timing 100000 iterations of C-style, push... C-style: 13 wallclock secs (12.97 usr + 0.00 sys = 12.97 CPU) @ 77 +10.10/s (n=100000) push: 18 wallclock secs (18.79 usr + 0.00 sys = 18.79 CPU) @ 53 +21.98/s (n=100000)

Replies are listed 'Best First'.
Re: Answer: How can I skip an element in an array whilst reading...
by belg4mit (Prior) on Apr 07, 2002 at 18:19 UTC
    Funny, I get
    q( Benchmark: timing 100000 iterations of C-style, push... C-style: 2 wallclock secs ( 3.20 usr + 0.00 sys = 3.20 CPU) @ 31 +279.32/s (n=100000) push: 3 wallclock secs ( 2.63 usr + 0.09 sys = 2.72 CPU) @ 36 +724.20/s (n=100000)
    I even went so far as to gather:
    #These "cripple" C-style and push to make it a fair fight, by reinitia +lizing the array each time Benchmark: timing 100000 iterations of C-style, destroy, push... C-style: 4 wallclock secs ( 3.88 usr + 0.00 sys = 3.88 CPU) @ 25 +779.84/s (n=100000) destroy: 2 wallclock secs ( 2.25 usr + 0.08 sys = 2.33 CPU) @ 42 +918.45/s (n=100000) push: 4 wallclock secs ( 3.24 usr + 0.07 sys = 3.31 CPU) @ 30 +184.12/s (n=100000) #These do not: Benchmark: timing 1000000 iterations of C-style, destroy, push... C-style: 34 wallclock secs (32.64 usr + 0.00 sys = 32.64 CPU) @ 30 +633.50/s (n=1000000) destroy: 24 wallclock secs (22.72 usr + 0.81 sys = 23.53 CPU) @ 42 +498.94/s (n=1000000) push: 2 wallclock secs ( 2.38 usr + 0.00 sys = 2.38 CPU) @ 41 +9463.09/s(n=1000000) );
    @in = (1,2,3,4,5,6,7,8,9,0); use Benchmark; timethese(100000, { 'C-style' => '#@in = (1,2,3,4,5,6,7,8,9,0); for my $i ( 0 .. $#in ) { $i % 2 ? $out2[int($i/2)] = $in[$i] : $out1[$i/2] = $in[$ +i]; }', 'push' => '#@in = (1,2,3,4,5,6,7,8,9,0); for my $i ( 0 .. $#in ) { $i % 2 ? push @out2, $in[$i] : push @out1, $in[$i]; }', 'destroy' => '@in = (1,2,3,4,5,6,7,8,9,0); while( @in ){ push @out1, shift(@in); push @out2, shift(@in); }' });
    Statistics can say lots of things
    q( #These "cripple" C-style and push to make it a fair fight, by reinitia +lizing the array each time C-style: 1 wallclock secs ( 0.57 usr + 0.04 sys = 0.60 CPU) +@ 16528.93/s (n=10000) destroy: 337 wallclock secs (325.85 usr + 0.28 sys = 326.13 CPU) +@ 30.66/s (n=10000) push: 1033 wallclock secs (983.44 usr + 0.85 sys = 984.30 CPU) +@ 10.16/s (n=10000) #These do not Benchmark: timing 10000 iterations of C-style, destroy, push... C-style: 1 wallclock secs ( 0.47 usr + 0.03 sys = 0.51 CPU) @ + 19762.85/s (n=10000) destroy: 339 wallclock secs (321.60 usr + 0.32 sys = 321.92 CPU) @ + 31.06/s (n=10000) push: 655 wallclock secs (641.02 usr + 0.54 sys = 641.56 CPU) @ + 15.59/s (n=10000) ); open OUT, ">/dev/null"; select(OUT); $|++; select(STDOUT); @in = (1,2,3,4,5,6,7,8,9,0); use Benchmark; timethese(10000, { 'C-style' => '#@in = (1,2,3,4,5,6,7,8,9,0); for my $i ( 0 .. $#in ) { $i % 2 ? $out2[int($i/2)] = $in[$i] : $out1[$i/2] = $in[$ +i]; } print OUT @out1,"\n"; print OUT @out2,"\n";', 'push' => '#@in = (1,2,3,4,5,6,7,8,9,0); for my $i ( 0 .. $#in ) { $i % 2 ? push @out2, $in[$i] : push @out1, $in[$i]; } print OUT @out1,"\n"; print OUT @out2,"\n";', 'destroy' => '@in = (1,2,3,4,5,6,7,8,9,0); while( @in ){ push @out1, shift(@in); push @out2, shift(@in); } print OUT @out1,"\n"; print OUT @out2,"\n";' });

    --
    perl -pe "s/\b;([st])/'\1/mg"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://157254]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-29 10:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found