Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Ampersands and sub speed

by diotalevi (Canon)
on Oct 14, 2005 at 21:29 UTC ( [id://500376]=note: print w/replies, xml ) Need Help??


in reply to Ampersands and sub speed

Your detected difference between &do_nothing; and do_nothing/do_nothing()/&do_nothing() is because &do_nothing; is special. &do_nothing; makes a call to do_nothing and re-uses the @_ from the calling subroutine. Its as if you'd said do_nothing( @_ ) except that it has lower overhead. In real code you'll almost never want to use the &do_nothing; syntax because it surprises people when they see it. You were. You didn't know that you were passing @_ into the argument list of do_nothing.

You should take into account the method cache and the two different types of method call. The method cache is invalidated any time a sub is added or removed from a package, anywhere. So all uses of *$foo = sub { ... } invalidate this cache. So does eval "sub bar { ... }". Actually changing any symbol tables, like deleting them or such also does this. Assigning to any @ISA anywhere also invalidates this cache.

Usually you try to avoid doing those sorts of things during perl's execution so that rarely matters. Once a named method has been called it is added to the method cache. When a method is called that is not in the method cache, perl consults @ISA until it finds the method. Once found, it adds it to that hash. This means there's a clear difference in cost between repeated method calls where the cache is able to be used and things where the cache is not used. Normal perl code usually uses the cache.

Your two different method call types are LHS->method_name and LHS->$method. Each uses a different opnode. In my example, LHS could have been a class or an object. Its immaterial.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://500376]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 18:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found