Three Tips to Help Optimize Your Code.
This may not boost your program's speed tenfold (in fact, some compilers out there already take care of a lot of optimization behind the scenes), but I'm a firm believer in the idea that every bit counts and that knowledge is power. So, without further ado, here are a few tips that may be more or less relevant to my fellow novice programmers: Code Motion ("Precomputation") This is a fairly simple thing to catch. For a simple example, let's say you have a for-loop that reads: for (int i = 0; i < someString.length(); i++) Every time your program runs that loop, it will call someString.length(). The easy solution to this is to define the length just before the loop and store it in a variable: int len = someString.length(); for (int i = 0; i < len; i++) And just like that, you only call someString.length() once! On modern day hardware, this doesn't always make a huge difference (unless you have a humongous string), but for more intensive algorithms - or if the loop happens to be called a lot - there will be a noticeable difference in your program's speed. Share Common Subexpressions Let's say you have a few variables being computed: int i = (q * p) + 1; int j = (q * p) + 2; int k = (q * p) + 3; // etc... Now, it may seem messy to want to add another variable on that list, but it improves program performance if you assign (q * p) to a variable beforehand, so your code reads more like this: int qp = (q * p); int i = qp + 1; int j = qp + 2; int k = qp + 3; // etc... And just like that, you've improved your program's performance. Know Your Language - Row Major or Column Major? This tends to make a big difference if you're working with nested loops and a data structure such as a 2D array. If I said to imagine drawing out a picture of a 2D array, you would imagine it would have n columns and m rows, right? But on computers, a 2D array isn't stored as a rectangle in memory - it's stored as a continuous row of data. Depending on your language, it may store this data with the rows as one giant row (row-major - languages such as C++ and Java), or the columns as one giant row (column-major - languages such as FORTRAN and MATLAB). To illustrate, if we have a 2x2 2D array: a b c d This would then be stored as: Row major: a b c d Column major: a c b d Let's say we're working in C (because we're masochists and like low-level languages), which is row-major. If we have a loop such as: // Psuedo-code given a 2D array, arr from i = 0 to 2048 from j = 0 to 2048 arr[i][j] = (i + 1) * (j + 2) This will run noticeably faster than: // j-loop first, then i-loop from j = 0 to 2048 from i = 0 to 2048 arr[i][j] = (i + 1) * (j + 2) And vice-versa for column-major languages. I know this was a long point to illustrate, and it may seem a little jumbled, but it's not too bad - I promise (you may have even worked with nested loops this way prior discovering this). So, with that, hopefully this will help some of you guys looking to fine-tune your code. There are many more ways to optimize your program than this, but I hope the points I discussed are something you can remember and, with any luck, turn into a habit. Happy coding!









