in reply to Re: Re: *Fastest* way to print a hash sorted by value
in thread *Fastest* way to print a hash sorted by value

The hidden trap in this suggestion is
if(scalar keys %HASH <= $keep) {
The risk is that if %HASH really does hold 10 million entries, and you haven't yet blown memory and started thrashing badly, loading all of the keys into a new temporary array will push you over the edge. (Unless Perl is clever enough to optimize this case, which I don't believe it is. Does anyone know otherwise?)
See update.

Virtual memory is a real nuisance. A lot of schemes that work fast on paper collapse entirely once you start having to start swapping memory to disk.

Update: BrowserUk has graciously pointed out that keys in scalar context returns the number of keys.

Replies are listed 'Best First'.
Re: Re: Re: Re: *Fastest* way to print a hash sorted by value
by bart (Canon) on Aug 09, 2003 at 22:20 UTC
    I can imagine that calling keys in scalar context for a tied hash is really slow — as slow as the loop for the actual job, later on. Therefore, perhaps I should keep a running counter, and postpone the test until after everything is done. The season for the test is really because I collect $keep items without sorting them. In this case, we need a final sort.

    Perhaps we can do a final sort in any case, and skip keeping the counter.