laziness, impatience, and hubris | |
PerlMonks |
Re: Hash Search is VERY slowby choroba (Cardinal) |
on Sep 28, 2021 at 21:50 UTC ( [id://11137098]=note: print w/replies, xml ) | Need Help?? |
When working with a large file, don't read the whole file into an array. Process the file line by line. Replace
by
Also, when populating the hash, Perl can do most of the work for you: This line can replace the whole if..else... construct. If the key doesn't exist in the hash, it will be created, and the dereference will create an anonymous array to which the url will be pushed. If the key exists, the url is simply pushed to the referenced array. Whether it's also responsible for the slowdown, I have no idea. Another place where you can avoid creating a temporary variable is the splitting. You can populate the two variables directly by subscripting the result of split:
I used the following to generate the input:
This was the benchmark:
This is the result on my machine:
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
In Section
Seekers of Perl Wisdom
|
|