Optimizing code

From Dreamwidth Notes
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.


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 ) = @_;


Database, Memcache, etc


Other discussion