IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Things in lists'n'stuff.
Is this the fastest way of determining whether $thing is in @array?

If (map {/$thing/} @array) { do stuff; }

It seems feary slow to me, but mebbe that's just the way things are.


Peter
[link|http://www.ubuntulinux.org|Ubuntu Linux]
[link|http://www.kuro5hin.org|There is no K5 Cabal]
[link|http://guildenstern.dyndns.org|Home]
Use P2P for legitimate purposes!
New Re: Things in lists'n'stuff.
That snippet (with "map" changed to "grep") will match substrings, giving you false positives (unless that's what you want). Try

if (grep {$_ eq $thing} @array) { do stuff; }

if you're probing once. If you're going to probe multiple times, turn @array into a hash, and do

if ($hashed_array{$thing}) { do stuff; }

New Even if only once....
if you're looking in an array (for an exact match), you probably want the hash.
Expand Edited by Simon_Jester March 30, 2005, 04:39:38 PM EST
New Only if you're going to repeat the operation a few times
Building a hash is slower than scanning the array.

Accessing a hash many times is faster than scanning the array many times.

At some fairly small fixed number, building the hash is worthwhile. That number varies with OS, version of Perl, and what parameters Perl was built with. I tested it once at 7, but that is probably not true any more.

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
     Things in lists'n'stuff. - (pwhysall) - (3)
         Re: Things in lists'n'stuff. - (dws) - (2)
             Even if only once.... - (Simon_Jester) - (1)
                 Only if you're going to repeat the operation a few times - (ben_tilly)

We're not going to play any mature games, are we?
40 ms