Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

local vs. $self-> pointer to subroutine

by ISAI student (Scribe)
on May 23, 2011 at 15:44 UTC ( #906313=perlquestion: print w/ replies, xml ) Need Help??
ISAI student has asked for the wisdom of the Perl Monks concerning the following question:

Hello all. I can benchmark this, but I want to know if what I am doing makes sense. It pertains to the PERL 5.8.8 I have a module, and I have a foreach loop inside one of the modules which calls on sub isInst. So I run:
foreach my $var (@something) { $self->isInst($var) }
However, it could be faster (it is in Python) to do the following:
my $func_Ref=\&$self->isInst; foreach my $var (@something) { &$func_Ref($var) }
Same may be done for other references, which need be accessed via the scalar $self. From knowing the internals of the PERL 5.8.8 engine, should there be any difference? Are there any known reccomendations? I could not find any.

Comment on local vs. $self-> pointer to subroutine
Select or Download Code
Re: local vs. $self-> pointer to subroutine
by Corion (Pope) on May 23, 2011 at 15:55 UTC

    Your Python approach does not work in Perl, because Perl does not have "bound" subroutines like Python has.

    You can, I presume, gain a very, very slim advantage by prefetching the subroutine method via $obj->can(), and then call that subroutine method with $self as its first parameter. But you'll need to benchmark that for your specific situation.

    my $isInst = $self->can('isInst'); for my $var (@something) { $isInst->($self,$var); };

    I believe that at least while you don't rearrange or change the class hierarchy (that is, write-access to any @ISA), Perl will cache these methods anyway, at least in later versions than Perl 5.8.8, which is two major versions behind the current supported version. In that situation, this idea will likely be slower than direct method calls in Perl.

Re: local vs. $self-> pointer to subroutine
by BrowserUk (Pope) on May 23, 2011 at 15:59 UTC

    At the very least, if isInst is a method, then it will be expecting $self as its first argument, so you'd need to do

    my $func_Ref = \&$self->isInst; foreach my $var ( @something ) { &$func_Ref( $self, $var ) }

    Or better (IMO):

    my $func_Ref = \&$self->isInst; foreach my $var ( @something ) { $func_Ref->( $self, $var ); }

    But do profile to ensure this is your bottleneck, and benchmark to verify the gains are sufficient to warrant the obfuscation.

    And if the performance of this loop is important enough to consider tolerating that level of obfuscation, you can gain a bit more by using a postfix loop:

    my $func_Ref = \&$self->isInst; $func_Ref->( $self, $_ ) for @something;

    I'd use the postfix loop here anyway.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: local vs. $self-> pointer to subroutine
by LanX (Canon) on May 23, 2011 at 17:13 UTC
    Perl has a special - widely unknown - syntax for this:

    my $func_Ref = \&$self->isInst; $self->$func_Ref( ... );

    see OOP: Obj->Coderef for calling Private Methods for details.

    Cheers Rolf

    PS: But I'm not sure if the performance win does worth it. AFAIK Perl does a lot of optimization like method caching to speed things up.

    UPDATE: typo fixed

      What are the criteria for calling it wildly unknown, that you didn't know it? :)
        Hi BUK!

        > What are the criteria for calling it wildly unknown

        Simple! Practically nobody mentions it.

        Search the archives and prove me wrong...

        Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://906313]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-11-29 01:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (201 votes), past polls