Monday, March 27, 2017

My trinity of code optimization

In conservation, there is the trinity "reduce, reuse, recycle".

In code optimization, I also have a trinity: code, data, results. Basically, it is about balancing the reuse of these three.

Reuse code. Your code should be as short as possible, and that means reusing as much as you can. But this should not be at the expense of code readability, unless you really really need that small pinch of extra speed.

Reuse data. Once data has been loaded, reuse it as much as you can so that you maximize the I/O cost of loading that data in the first place. Of course, this must be balanced with the amount of data you keep in memory, with the actual amount of memory that you have. On most modern computers, for most day-to-day tasks, this should not be a problem, but for complex computing problems with large datasets, this may not be as easy as it seems.

Reuse results. There is a computing (and maybe I/O) cost to calculating results. So when possible, results should be kept in memory so that they can be reused when necessary, instead of recomputing them. Again, this needs to be balanced with the amount of memory you have.

So there you have it. My trinity of code optimization, which is basically a balance between reusing code, data, and results. But though it may sound easy, achieving a good balance is actually not an easy thing to do.

No comments: