Carlos
Carlos

Reputation: 5

Memory usage perl function

There's any difference, concerning to memory and/or disc usage, between these two ways of passing variables to a subroutine:

&subrutine_1($hash_reference);

sub subrutine
{
    my $hash = $_[0];
    my $var_1 = $$hash{'var_1'};
    my $var_2 = $$hash{'var_2'};
    my $var_3 = $$hash{'var_3'};
}

or

&subrutine_1($hash_reference);

sub subrutine
{
    my $var_1 = $_[0]{'var_1'};
    my $var_2 = $_[0]{'var_2'};
    my $var_3 = $_[0]{'var_3'};
}

Thanks!

Upvotes: 0

Views: 102

Answers (1)

choroba
choroba

Reputation: 241748

Disk usage should be the same, as there's no I/O operation. Memory consumption will be larger in the first case, because you need one more scalar variable $hash. It will only store a reference, so the difference is minimal.

Really copying the hash can consume a lot more memory, though:

sub subroutine {
    my %hash = %{ $_[0] };
    my $var_1 = $hash{var_1};
    # ...
}

Upvotes: 1

Related Questions