in reply to spinner to keep the patiently waiting user patient

That's a cool way to do it. Easy to follow, too, which is a big plus.

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.


Mike

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
    For longer running code in a tight loop, you also need to be aware that at some point $ndx++ stops working. For instance, with normal IEEE doubles:
    $ perl -we'$x = $y = 2**53-1; ++$x; print $x-$y' 1 $ perl -we'$x = $y = 2**53; ++$x; print $x-$y' 0
    On my computer, in a tight loop, you could reach this point in just 30 years :)
      Oh well, if your loop is a bit looser, Y2038 could get you before 2^53 could. :)

      Mike
        Guys, guys, guys! If your spinner is still going after that long a time, I think you have more problems to worry about than counter overflow.

        :-)

         

        perl -le "print+unpack'N',pack'B32','00000000000000000000001010011000'"

Re^2: spinner to keep the patiently waiting user patient
by Aristotle (Chancellor) on Dec 09, 2003 at 12:40 UTC
    You have a point, but.. Running your benchmark on my machine gives me a rate of over 900,000 shift spins per sec. That is about 45,000 times faster than I need it.

    Makeshifts last the longest.