Difference between revisions of "Optimizing code"
m (Forgot a closing tag, oops.) |
Shadowspar (Talk | contribs) (+caveat: pls don't stress over performance, at least not up front) |
||
(One intermediate revision by one other user 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. | ||
− | + | '''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. | |
− | The <code>shift()</code> function, used to | + | 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 short functions (1-5 lines), use <code>$_[0]</code> for the first argument, <code>$_[1]</code> for the second, etc. | ||
Line 11: | Line 19: | ||
([http://bugs.dwscoalition.org/show_bug.cgi?id=1367 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