[Melbourne-pm] passing arrays and hashes
Tim Connors
tconnors at astro.swin.edu.au
Sun Aug 5 20:08:16 PDT 2007
I thought I was doing the proper thing by passsing a reference to a hash
and dereferncing it in a function, rather than passing the entire hash
around the place all the time.
sub blah($$$) {
my ($file,$md5sum,$trashmd5p)=(@_);
my %trashmd5=%$trashmd5p;
...
}
blah($file,$md5sum,\%trashmd5) ...
I found this much much slower than I was expecting. As an experiment, I
decided just to pass the hash straight to the function:
sub blah($$%) {
my ($file,$md5sum,%trashmd5)=(@_);
...
}
blah($file,$md5sum,%trashmd5) ...
This was a bit quicker.
And of course, just using a global was very quick.
I think I can see why passing the reference is slow -- perl wants to go
through and create a whole new hash in this stage creating a duplicate
reference to every item in %$trashmd5p:
my %trashmd5=%$trashmd5p;
It doesn't realise %trashmd5 is only a readonly copy, and that it would
suffice simply to derefence it just like in C.
Having found my workaround, and this being part of a very long running
script, I don't particularly feel the want to experiment much more, but
what is the way around this? Being a readonly copy, can I simply adopt
the slightly more verbose referencing to ${$trashmd5p}{key} everytime
instead of $trashmd5{key}, and this wouldn't go and recreate a fresh hash
from scratch?
--
Tim Connors
More information about the Melbourne-pm
mailing list