There's more than one way to do things PerlMonks

How to traverse an array as fast as possible?

by llancet (Friar)
 on Jun 17, 2009 at 06:02 UTC Need Help??

llancet has asked for the wisdom of the Perl Monks concerning the following question:

Which way is fastest?
```
while (my \$value=shift @array)
while (my \$value=pop @array)
foreach my \$value (@array)

# this might be the slowest
for (my \$i=0;\$i<=@array;\$i++) {
my \$value=\$array[\$i];
}

Replies are listed 'Best First'.
Re: How to traverse an array as fast as possible?
by moritz (Cardinal) on Jun 17, 2009 at 06:14 UTC
Benchmark is your friend - find out for yourself.

(I expect for my \$value (@array) to be the fastest, because it does the least unnecessary extra operations, but that's really just guessing)

Re: How to traverse an array as fast as possible?
by ciderpunx (Vicar) on Jun 17, 2009 at 07:29 UTC
foreach on my machine.
```#!/usr/bin/perl
use warnings;
use strict;
use Benchmark qw(:all);

my @bigarray;
for(1..100000) {
push @bigarray, "A randomish string for testing arrays with realisti
+c data" ;
}

timethese(100, {
while_shift => sub{
my @array = @bigarray;
while (my \$value=shift @array){}
},
while_pop   => sub{
my @array = @bigarray;
while (my \$value=pop @array){}
},
for_each    => sub{
my @array = @bigarray;
foreach my \$value (@array){}
},
for_loop    => sub{
my @array = @bigarray;
for (my \$i=0;\$i<=@array;\$i++) {
my \$value=\$array[\$i];
}},
});
Gives me:
```\$ perl a.pl
Benchmark: timing 100 iterations of for_each, for_loop, while_pop, whi
+le_shift...
for_each:  4 wallclock secs ( 3.85 usr +  0.01 sys =  3.86 CPU) @ 25
+.91/s (n=100)
for_loop:  7 wallclock secs ( 7.36 usr +  0.01 sys =  7.37 CPU) @ 13
+.57/s (n=100)
while_pop:  5 wallclock secs ( 5.35 usr +  0.00 sys =  5.35 CPU) @ 18
+.69/s (n=100)
while_shift:  5 wallclock secs ( 5.38 usr +  0.00 sys =  5.38 CPU) @ 1
+8.59/s (n=100)
--
Linux, perl, punk rock, cider: charlieharvey.org.uk.
Re: How to traverse an array as fast as possible?
by graff (Chancellor) on Jun 17, 2009 at 06:25 UTC
So, are you asking monks to write a Benchmark script for you? I'm not trying to be critical -- I think it's true that writing a valid test for this question would involve some subtle issues, to make sure that the right things are being tested. I'm not sure I'm up to the task (though I would predict that the "foreach (@array)" loop would do best, since it doesn't have to change the array or copy array values as it iterates).

I think it's also true that whatever difference there might be among those alternatives, it will be impressively insignificant -- it will count for nearly nothing -- when seen in the context of any real-life script that actually does something useful within the while or for loop.

Re: How to traverse an array as fast as possible?
by Anonymous Monk on Jun 17, 2009 at 06:15 UTC
Re: How to traverse an array as fast as possible?
by llancet (Friar) on Jun 17, 2009 at 09:03 UTC
Thanks to everyone above, although I've fount that my problem is not about time consumption of array accession. I have to make another new node...

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://772237]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-07-14 10:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?
 • erzuuli ‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.