Optimizing code

From Dreamwidth Notes
Revision as of 12:09, 16 March 2013 by Shadowspar (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

https://github.com/swaldman3/dw-free/commit/242c54bde6184b5fa8a45090256eb0b5c818307c#cgi-bin-lj-user-pm-P10


Other discussion

http://dw-dev.dreamwidth.org/81634.html