Difference between revisions of "Optimizing code"

From Dreamwidth Notes
Jump to: navigation, search
(Created page with 'This is a page on code optimization--practices that encourage efficient code. == Avoid shift == Shifting to get arguments from <code>@_</code> is slower than using <code>$_[0]<...')
 
(Rewriting a bit.)
Line 3: Line 3:
 
== Avoid shift ==
 
== Avoid shift ==
  
Shifting to get arguments from <code>@_</code> is slower than using <code>$_[0]</code> (for short functions of 1-5 lines) or <code>my ( $foo, $bar ) = @_;</code> (for longer functions). ([http://bugs.dwscoalition.org/show_bug.cgi?id=1367 source])
+
The <code>shift()</code>> function, used to take the first argument from the <code>@_</code> list, is slow. Instead:
 +
 
 +
* For short functions (1-5 lines), use <code>$_[0]</code> for the first argument, <code>$_[1] for the second, etc.
 +
* For longer functions, use the following construct to assign the values to named scalars:
 +
 
 +
<source lang="perl">my ( $foo, $bar ) = @_;</source>
 +
 
 +
([http://bugs.dwscoalition.org/show_bug.cgi?id=1367 source])
  
 
[[Category: Development]]
 
[[Category: Development]]

Revision as of 17:51, 6 July 2009

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

Avoid shift

The shift()> function, used to take the first argument from the @_ list, is slow. 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)