Friday, 25 March 2016

Week 10 - Efficiency

Getting the code to work the way you want and get the job done is just half way down the road to accomplishing a task. The rest of the way is making it efficient. This is because we don't have an unlimited supply of memory or time. As our hard drive capacity increases, so does our usage, so it becomes vital to create a program that does a task in such a way that it uses only as much resources as is absolutely necessary and does the task quickly. This is precisely what I learned this week, how to make my code more efficient.

This week, in our second assignment as well as in lectures, the need for a code to be efficient was stressed. In my assignment on solving puzzles, initially my program was running forever to solve a 9x9 sudoku puzzle. After attending this week's lectures, I thought through my algorithm again to see where I could make it more efficient. One thing we learned is to avoid duplication. For example, when doing recursion, there is a lot of room for the same value to be computed more than once. This is a waste of time and resources. To avoid this, one can use a data structure to store the values already computed and make the program check to see if the value has already been found before proceeding to compute it (whether recursively or not).  This saved me a lot of time and made my program solve the same 9x9 sudoku puzzle in 15 seconds!

A simple task such as storing information in a data structure can go a long way in saving a lot of time and energy. This really amazes me and makes me eager to learn more such techniques to make my code more efficient.

No comments:

Post a Comment