in reply to spinner to keep the patiently waiting user patient
One thing you have to keep in mind, though, is that shift and push are expensive. It's a lot cheaper to index++ % @spin.
It doesn't matter for this code, since you're only going to shift/push once per second, but you might want to keep it in mind if you ever need to do this kind of array looping in a tighter loop.
ObBenchmark:
#!/usr/bin/perl -w use strict; use Benchmark qw(cmpthese); my $i=0; my @spin=qw(- \\ | /); sub spinShift { my $spin=shift @spin; push @spin,$spin; return $spin; } my $ndx=0; sub spinNdx { return $spin[$ndx++ % @spin]; } cmpthese(-2, { spinShift => \&spinShift, spinNdx => \&spinNdx, } );
On a perl 5.8 Linux system, using an index clocks in 133% faster than using shift/push.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: spinner to keep the patiently waiting user patient
by ysth (Canon) on Dec 08, 2003 at 18:12 UTC | |
by RMGir (Prior) on Dec 08, 2003 at 19:26 UTC | |
by rob_au (Abbot) on Dec 08, 2003 at 22:05 UTC | |
|
Re^2: spinner to keep the patiently waiting user patient
by Aristotle (Chancellor) on Dec 09, 2003 at 12:40 UTC |