in reply to Re: Anonymous or normal hashes?
in thread Anonymous or normal hashes?

Well, in prototyped version (\%) i will get a reference. However nothing would stop me from doing same sub with ($) and using that to pass a reference (in this case hash_ref). Now that would not be nice to the interface of that sub (eq. is actually a hash_ref instead of scalar), but would it then be faster?

What i'm hoping here is that by using prototypes (and \%) perl is not transfering a hash as whole but a reference to hash, which it seems from my limited understanding.

Replies are listed 'Best First'.
Re: Re: Re: Anonymous or normal hashes?
by duff (Parson) on Jan 09, 2004 at 16:07 UTC

    You could test this yourself using top(1) or whatever the quivalent is on Windows if you're on that platform. There's also a module that will tell you how much memory your data structures are using, but I forget the name of it.

    Simple tests of my own show that when I create a large hash, the program uses about 71M. When I pass it as a hash ref (sub foo { ...}  foo(\%hash);), the program still uses about 71M. When I pass it as a hash (sub foo { ...}  foo(%hash);), the program uses about 117M. When I pass it as a hash to a prototyped sub ((sub foo (\%) { ...}  foo(%hash);), the program uses about 117M. Of course, I could have screwed up so test it yourself! :-)

    Here's the program I used while I had top(1) running in another window. I just ran it 3 times with two of the three alternatives commented out each time and while it was sleeping I looked at the top window.

    #!/usr/bin/perl print "creating hash\n"; $k = "aaaaaaaa"; for (0..1000000) { $hash{$k++} = $_ }; print "sleeping\n"; sleep (10); print "copying\n"; foo(\%hash); #bar(%hash); #baz(%hash); print "done"; exit; sub foo { print "sleeping in foo\n"; sleep(10); } sub bar (\%) { print "sleeping in bar\n"; sleep(10); } sub baz (%) { print "sleeping in baz\n"; sleep(10); }
Re: Re: Re: Anonymous or normal hashes?
by ysth (Canon) on Jan 09, 2004 at 16:33 UTC
    With:
    sub foo (\%) { ... } foo(%$hashref);
    perl doesn't have to copy or expand the hash, but it will generate a new reference to it.