Difference between revisions of "Optimizing code"
(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]<...') |
Shadowspar (Talk | contribs) (+caveat: pls don't stress over performance, at least not up front) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
This is a page on code optimization--practices that encourage efficient code. | This is a page on code optimization--practices that encourage efficient code. | ||
− | == Avoid shift == | + | '''Caveat''': While this page details ways to improve performance, ''please'' don't let it become a primary consideration when first writing code! Clarity and performance often come at the expense of one another, and making elaborate tweaks that turn out to be unnecessary isn't a good use of anyone's time. |
+ | |||
+ | Please don't hold back a possible contribution because you're worried about performance. In the rare case that something does need to be tuned up, our ace code review team will pick up on it and work with you to make whatever tweaks might be necessary. | ||
+ | |||
+ | |||
+ | |||
+ | == Perl == | ||
+ | |||
+ | === Avoid shift === | ||
+ | |||
+ | 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 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]) | ||
+ | |||
+ | == Database, Memcache, etc == | ||
+ | |||
+ | https://github.com/swaldman3/dw-free/commit/242c54bde6184b5fa8a45090256eb0b5c818307c#cgi-bin-lj-user-pm-P10 | ||
+ | |||
+ | |||
+ | == Other discussion == | ||
+ | |||
+ | http://dw-dev.dreamwidth.org/81634.html | ||
− | |||
[[Category: Development]] | [[Category: Development]] |
Latest revision as of 12:09, 16 March 2013
This is a page on code optimization--practices that encourage efficient code.
Caveat: While this page details ways to improve performance, please don't let it become a primary consideration when first writing code! Clarity and performance often come at the expense of one another, and making elaborate tweaks that turn out to be unnecessary isn't a good use of anyone's time.
Please don't hold back a possible contribution because you're worried about performance. In the rare case that something does need to be tuned up, our ace code review team will pick up on it and work with you to make whatever tweaks might be necessary.
Perl
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)
Database, Memcache, etc