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.
In reply to Re: spinner to keep the patiently waiting user patient
by RMGir
in thread spinner to keep the patiently waiting user patient
by ysth
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |