Hi All,
Task
I am passing a array as a reference to a subroutine which does some checking and returns a scalar.
Method1:
my $variable = my_subroutine(\@my_array);
sub my_subroutine
{
my $array = shift;
# involves dereferencing the array_reference
...
return(\$variable);
}
I get the expected output. Array dereferencing can be quite expensive and I wanted to refine the code further.
Method2:
I thought that I can gain some speed by passing just the array indices and access the array defined outside the subroutine as a global array. Again, I get the expected result.
my $variable = my_subroutine(\$array_start_idx, \$array_end_idx);
sub my_subroutine
{
my $start_idx = shift;
my $end_idx = shift;
# involves accessong the global array using the indices passed
...
return(\$variable);
}
Problem
When I use
Devel-NYTProf to profile both methods, passing the whole array as an array reference is much faster than passing the array indices. I was expecting the opposite of this!
I have to emphasize that innards of the subroutine is dereferencing an array reference in the first option and accessing the global array using the array indices passed to the subroutine.
Any insight into this is greatly apppreciated!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.