Chapter 12.3

Efficiency of a Function

AmazonEfficiency refers to the time complexity of a function. If two functions achieve the same purpose but one is significantly faster, it is always more desired. Efficiency can hurt code size and code size can hurt efficiency; it’s crucial to find a balance between them.

Usually the faster the function runs the better. An exception is that if your function runs fast but requires a huge memory footprint then it may not suit the platform you are using. This case is rare; so it usually doesn’t apply.It usually takes technical knowledge to achieve great efficiency, especially in advanced data structures. For instance, a binary search tree generally offers greater efficiency than a linear search mechanism such as an array; however, in its worst cases, the binary search tree’s efficiency is no better than the linear method’s. Experienced software engineers may choose to use another data structure such as a balanced binary search tree (e.g. an AVL tree) despite its rather complicated design.

Many data structures have been designed and implemented over the past few decades, and each of them has its advantages and disadvantages.The design of a function also has a significant impact on its efficiency. It includes the means to store data, the techniques to solve a task, the function’s flow control, and so on.

For example suppose your function has a big switch statement (or alternatively a series of if-else statements) which simply assigns a variable to some value given some condition. Imagine how many compare, call, jump instructions need to be created when the program is running!

If you use a map-like or hash table-like data structure in C++ then you can create a static mapping between keys and values and be able to do this task much faster and much more efficiently! Proficiency comes with experience with the C++ language as well as data structures.

Another way to increase efficiency of a program is to be familiar with C++ standard library. If you know exactly how its functions are implemented, you know their time complexities. For example, the containers provided by STL have many operations in common, but they have different time complexities. The deque template class supports insertion and removal of data items in constant time, whereas vector takes linear time.

If you are familiar with their implementations and your program's most common use cases, then when it comes time to determining which functions or techniques to use, you will be able to make wise decisions.

As you can see, achieving maximum efficiency of a function basically means you have to know the language extremely well. Many programmers are not too much concerned with the efficiency of a function because they think as long as it works and does not take ridiculously long, it’s fine.

However, if you write a function that handles a humongous set of data you will notice the significance of data structure and design, and thus realize the importance of efficiency.