Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Re: Array sort - kind of...

by Anonymous Monk
on Feb 04, 2002 at 02:07 UTC ( [id://143144]=note: print w/replies, xml ) Need Help??


in reply to Re: Array sort - kind of...
in thread Array sort - kind of...

This isn't relevant at all for the issue, but I have a question: Why do you choose to have $i over $_? There is a slight performance improvement when the loop is entered, but still...

Replies are listed 'Best First'.
Re: Re: Re: Array sort - kind of...
by demerphq (Chancellor) on Feb 04, 2002 at 11:16 UTC
    True, he could have it written it in such a way to minimize a bunch of the minor overheads of the loop construct.

    Thus

    my $min_i = 0; foreach my $i (1..$#array) { $min_i = $i if $array[$i]->[2] < $array[$min_i]->[2]; }
    becomes
    my $index=0; $array[$_][2]<$array[$index][2] && ($index=$_) foreach 1..$#array;
    But I'm so sure its easier to understand...

    ;-)

    Yves / DeMerphq
    --
    When to use Prototypes?

      Wouldn't this eliminate more of the overhead?
      my ($min, $min_i) = ($array[0]->[2], 0); foreach my $i (1..$#array) { ($min, $min_i) = ($array[$i]->[2], $i) if $array[$i]->[2] < $min; }


        p
        Yup, sure does. (*sigh* busted ;-) But it turns out a combination is slightly better as well. The modifier form of the for loop doesn't have the marginal overhead of scope (er I think...), although I wonder if the 3% gain is worth the obfu.

        I have to admit that I was suprised to discover that the list assignment is so much faster than an array lookup (which obviously is why yours is faster). Somthing I shall keep in mind for the future.

        Heres my benchmarks...

        Benchmark: running demerphq, petral, petral_modifier, each for at leas +t 60 CPU seconds... demerphq: 66 wallclock secs (62.98 usr + 0.00 sys = 62.98 CPU) @ 21 +2.26/s (n=13369) petral: 64 wallclock secs (62.99 usr + 0.00 sys = 62.99 CPU) @ 26 +7.17/s (n=16828) petral_modifier: 64 wallclock secs (63.17 usr + 0.00 sys = 63.17 CPU) + @ 274.43/s (n=17336) Rate demerphq petral petral_modifier demerphq 212/s -- -21% -23% petral 267/s 26% -- -3% petral_modifier 274/s 29% 3% --

        Yves / DeMerphq
        --
        When to use Prototypes?

Re: Re: Re: Array sort - kind of...
by wog (Curate) on Feb 04, 2002 at 02:13 UTC
    I find it clearer, since smitz referred to $i as the index to the array in the question and since $i, $j, etc. make me think "this is a counter variable" when I see them.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://143144]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found