From: Mr Hash <[EMAIL PROTECTED]>
> On 03/26, Jenda Krynicky said something like:
> > From: "Jonathan E. Paton" <[EMAIL PROTECTED]>
> > > >             affect the performance of:
> > > >   key listings (things like grep /pattern/, keys %hash)
> > > 
> > > Grep can be slow, especially with patterns.  Keys
> > > could be slow, especially if the dataset is large.
> > 
> > Maybe you could try to use
> > 
> >     while (my ($key) = each %hash) {
> >             next unless $key =~ /pattern/
> >             ... do whatever you need
> >     }
> 
> Hmm. I used grep thinking it would be fast. I'll try iterating the
> keys and having a good simple regex, too.

It IS pretty fast. But it you have lots of keys and do

        @good_keys = grep /pattern/, keys %hash;
or even
        @keys = keys %hash;

You force Perl to create a huge array for you.

I'm not sure what will Perl do if you

        foreach my $good_key (grep /pattern/, keys %hash) {

whether it'll create the temporary list or will behave "lazily" and grep 
through the keys as you need them, but I'd expect the first.

Jenda

=========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain
I can't find it.
                                        --- me

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to