Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Common Perl Idioms

by ihb (Deacon)
on Jul 24, 2004 at 00:45 UTC ( [id://377044]=note: print w/replies, xml ) Need Help??


in reply to Re: Common Perl Idioms
in thread Common Perl Idioms

An alternative to your &find_first_index that I prefer (for smaller lists) is

use List::Util 'first'; sub find_first_index { my $target = shift; first { $_[$_] eq $target } 0 .. $#_; } # Simple enough to write inline: my $idx = first { $ary[$_] eq $target } 0 .. $#ary;
I don't know about any efficiency issues though.

Update: Fixed a off-by-one bug in the sub and added parenthesis.

ihb

Read argumentation in its context!

Replies are listed 'Best First'.
Re^3: Common Perl Idioms
by demerphq (Chancellor) on Jul 24, 2004 at 02:47 UTC

    I don't know about any efficiency issues though.

    Doesn't look good:

    Rate ihb_zip ihb_hit dmq_zip dmq_hit ihb_zip 8461/s -- -5% -44% -50% ihb_hit 8869/s 5% -- -42% -47% dmq_zip 15177/s 79% 71% -- -10% dmq_hit 16782/s 98% 89% 11% --

    And thats using the XS List::Util implementation from 5.8.2. Theres a lot of overhead in calling the closure, and I think that the range operator isn't lazy in this usage, so it has to manufacture a list of indexes to use.


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


      sub firstIdx { $_[ $_ ] eq $_[ 0 ] and return --$_ for 1 .. @_; return; }

      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-23 17:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found