Difference between revisions of "Optimizing code"

From Dreamwidth Notes
Jump to: navigation, search
m (Forgot a closing tag, oops.)
(Avoid shift)
Line 3: Line 3:
 
== Avoid shift ==
 
== Avoid shift ==
  
The <code>shift()</code> function, used to take the first argument from the <code>@_</code> list, is slow. Instead:
+
The <code>shift()</code> function, used to get the first variable from arrays like the <code>@_</code> argument list for functions, is slow. Use these alternatives instead:
  
 
* For short functions (1-5 lines), use <code>$_[0]</code> for the first argument, <code>$_[1]</code> for the second, etc.
 
* For short functions (1-5 lines), use <code>$_[0]</code> for the first argument, <code>$_[1]</code> for the second, etc.

Revision as of 17:54, 6 July 2009

This is a page on code optimization--practices that encourage efficient code.

Avoid shift

The shift() function, used to get the first variable from arrays like the @_ argument list for functions, is slow. Use these alternatives instead:

  • For short functions (1-5 lines), use $_[0] for the first argument, $_[1] for the second, etc.
  • For longer functions, use the following construct to assign the values to named scalars:
my ( $foo, $bar ) = @_;

(source)