Perl Monk, Perl Meditation PerlMonks

### Re: How expensive is reverse?

by maverick (Curate)
 on Sep 01, 2001 at 21:35 UTC ( #109656=note: print w/replies, xml ) Need Help??

in reply to How expensive is reverse?

I'm not sure how they are implemented or what the (O) is (I suspect it's O(n)). Here's what a quick benchmark says from my Dual Celeron 400:
```
#!/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 },
}
);
Gives me this:
```
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)
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.

Updated CRAP! nardo is quite right. I hate it when I make silly mistakes.

/\/\averick
perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

Replies are listed 'Best First'.
Re: Re: How expensive is reverse?
by nardo (Friar) on Sep 01, 2001 at 22:08 UTC
ITYM
```@one   = (('a') x 100);
@two   = (('a') x 1000);
@three = (('a') x 10000);
As it is written each array has one element 100, 1000, or 10000 characters long rather than having 100, 1000, or 10000 elements one character long. With this change (and changing it to run 10,000 times) I get:
```Benchmark: timing 10000 iterations of rev 100, rev 1000, rev 10000...
rev 100:  0 wallclock secs ( 0.54 usr + -0.01 sys =  0.53 CPU) @ 18
+867.92/s (n=10000)
rev 1000:  4 wallclock secs ( 5.52 usr + -0.01 sys =  5.51 CPU) @ 18
+14.88/s (n=10000)
rev 10000: 59 wallclock secs (58.33 usr + -0.00 sys = 58.33 CPU) @ 17
+1.44/s (n=10000)

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://109656]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2022-07-07 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?