In: Computer Science
Runtime efficiency and code simplicity are often competing goals. How can you deal with this problem? Is it possible to have code that is both simple and efficient?
Runtime efficiency and code simplicity are often competing goals. How can you deal with this problem?
Run-time efficiency completely depends on the algorithm which a programmer is going to be implemented in his program. For example, there are two algorithms which can be implemented to for a piece of program with n inputs. And below is the table which shows the time taken for both algorithms depending on its input size.
n (list size) | Algorithm A run-time (in nanoseconds) |
Algorithm B run-time (in nanoseconds) |
---|---|---|
16 | 8 | 100,000 |
63 | 32 | 150,000 |
250 | 125 | 200,000 |
1,000 | 500 | 250,000 |
So, from the above table, it is clear that algorithm A is far superior in efficiency to that of algorithm B. So, to implement an algorithm we need to understand the basic intention of the program. Thus we can make sure to implement an algorithm which will be simple and optimized. A proper framework can be built to design the algorithm which will be highly efficient and simple. Big O notation is a convenient way to express the run time of an algorithm. Below approaches a programmer needs to follow while writing the code to make it both efficient and simple as well.
Is it possible to have code that is both simple and efficient?
Yes, it is possible to have code that is both simple and efficient. Code or algorithms that has a lot of statements, lot of nested loops, hard coded values of variables, poor naming convention, no comments, is often called a complex code. This type of code is not efficient as well. But by following above discussed approaches, we can write a simple and efficient code as well.