Sunday, August 23, 2009

Keep performance in mind

Almost anywhere you will find that your first step when coding is making it to work and only later worry about performance. I only half agree.

It's true that your code must work, otherwise you'll have nothing. But while you're coding you must always keep in mind the performance of this code. I don't mean that you should invest extra time trying to make your code faster, you just need to keep in mind some general recommendations about performance that will help you.

For Java developers, this is a good checklist, but most of them are valid for almost any language:
  1. Do not recalculate constants inside a loop.
  2. Reduce the number of network operations by returning complete results rather than smaller intermediate results.
  3. Reduce the distance between objects during operation. It is better to perform complex operations locally.
  4. Avoid object creation and destruction except as necessary. Reuse existing objects.
  5. Use open source frameworks which are established and tested.
Find a list of performance check points for your favorite language and keep it in mind.

See you soon.

4 comments:

  1. i have to disagree with avoiding creation of objects and re-using... to some extent.

    object creation/deletion is fast and if you save creating objects until absolutely nessicary, then it makes it easy to refactor, since all dependencies as close and likely in the same scope.

    ps: whats wrong with this Post a Comment text box... cant use alot of keys and cant copy/paste etc...

    ReplyDelete
  2. Hi Steven,

    I'll check what happens with the copy&paste, thanks for pointing it.

    Regarding the Object re-usage, I think it really depends of the situation and the type of Object. In general is considered bad practice reusing variables, so we should also consider reusing Objects another bad practice. But if the Object creation has a performance penalty, the we should think about reusing it ;)

    ReplyDelete
  3. Unless you're completely sure that you're working in a performance critical section of code, I don't think that many of the items of you've just listed are worth worrying about; for example, as Steven mentions, object creation can be fast enough. I believe it's far better, as a general rule, to concentrate on making the code obviously correct and maximising maintainability. The exception is when you're pretty sure ahead of time that there could be a measurable performance issue: minimising network traffic is indeed often one such consideration.

    ReplyDelete
  4. It depends on the type of application you are writing if object creation is a performance issue. My experience is that object creation can cause a big slowdown if you are doing million calls a second.

    A remark on the not recalculating constants in a loop; there is a compiler optimisation called loop invariant code motion that moves unnecessary calculations out of the loop.

    http://en.wikipedia.org/wiki/Loop-invariant_code_motion

    ReplyDelete