Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Why is my program so slow even though I've used hashes?

by Marshall (Canon)
on May 28, 2009 at 19:54 UTC ( [id://766736]=note: print w/replies, xml ) Need Help??


in reply to Why is my program so slow even though I've used hashes?

I don't have any sample data to work with, and I might be wrong, but even if I am, perhaps you will get some new ideas...

I think the problem starts with how the CSA_hash is built. You have 3 values: $pdb,$chain,$res that are concatenated together to form one key and the value of that key is always 1. That is all well and good if the main thing is that this combination of things is only counted once. Maybe you need to do that, I don't know. BUT later on in the most performance critical part of the code, what you really want to know is what $res's correspond to the $pdb,$chain combination. So another idea for CSA_hash, would be like this:

die "some usage error msg" if (@ARGV !=2); my ($domFile,$csafile)= @ARGV; open(CSAFILE, "$csafile") or die "unable to open $csafile: $!"; my %csa_hash; my %dom_hash; my $domain_dir = "<my directory>"; while(<CSAFILE>) { chomp; s/\s+//g; #keep things simple, delete spaces my ($pdb,$chain,$res) = (split(/,/, $_))[0,3,4]; push (@{$csa_hash{"$pdb;$chain"}},$res}); }
This is a HoL, Hash of List. Maybe there can be duplicate $res values for each "$pdb;$chain" combo? I don't know. If there are, then it is not hard to fix that.

Where all of this is headed is to the 3rd loop level:

while ((my $key, my $value) = each(%csa_hash)) {....}
note that $value here is irrelevant (it is always 1). %csa_hash is being used like a list NOT a hash! This is actually the worst possible case of a search: it is linear and it always access all N items in the list. If there are a lot of keys in %csa_hash, this will bog things down quite a bit! I think this is a very key statement:

if("$domchain" eq "$pdbchain"){....}
This $domchain is a function of just the stuff that was used to create the csa_hash! Or actually the modified version above. A hash is a horrible thing to waste...or so the saying goes... So I recommend created the csa_hash with keys that are easy for the inner most loop to access. This routine is going to get "hit a lot", but you want fast answer, not a N*keys(csa_hash) answer.

One thing that I think is noteworthy is that if a string can work in a print statement, it will work as a hash key, eg. $somehash{"$pdb;$chain"}= $res; If the ";" is not a valid value inside of the $parm vars, then this works great. If you have a multi-value key, you can do a split on ";".

hope this helps.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 04:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found