Gives me this:#!/usr/bin/perl use Benchmark; @one = ('a' x 100); @two = ('a' x 1000); @three = ('a' x 10000); timethese( 100000, { 'rev 100' => sub { reverse @one }, 'rev 1000' => sub { reverse @two }, 'rev 10000' => sub { reverse @three }, } );
At a 100,000 iterations of a 1,000 element list, it took 0.87 seconds of CPU time. Not too expensive I'd say.Benchmark: timing 100000 iterations of rev 100, rev 1000, rev 10000... rev 100: 1 wallclock secs ( 0.24 usr + 0.00 sys = 0.24 CPU) @ 41 +6666.67/s (n=100000) (warning: too few iterations for a reliable count) rev 1000: 1 wallclock secs ( 0.87 usr + 0.00 sys = 0.87 CPU) @ 11 +4942.53/s (n=100000) rev 10000: 8 wallclock secs ( 7.65 usr + 0.00 sys = 7.65 CPU) @ 13 +071.90/s (n=100000)
Updated CRAP! nardo is quite right. I hate it when I make silly mistakes.
/\/\averick
perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"
In reply to Re: How expensive is reverse?
by maverick
in thread How expensive is reverse?
by bless$self=>perlmonks;
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |