When programming in C++, one of the best key skills to learn is efficiency. Reasons why you would want to develop efficient programming skills are:
A. Applications run faster
B. Programs take up less space
C. Uses less power from the processor
The only con to efficient C++ programming is that it takes more time from the programmer. But hey, it’s a well worth tradeoff. Another factor in efficient C++ programming is choosing the right compiler, but you’ll see that later in here. This tutorial will be split up into 4 different sections, one containing simple examples of efficient coding, the second containing the choice of a proper compiler, third will be creating efficient functions, and lastly will be optimizing algorithms.

Simple examples of Efficient C++ programming (Compiled in BC 5.02):
1. Program A

#include <iostream.h>

int main (void) {
cout << “Hello World!”;
}
Build Time - .767 seconds, Program Size – 64.5KB.
2. Program B

#include <stdio.h>

int main (void) {
printf(“Hello World!”);
}
Result: Build Time - .522 seconds, Program Size – 51.5 KB. **Note that this is actually C programming, but is more efficient in this case** (The reason the program is smaller is because the C printf/scanf is a lot smaller than C++ iostreams)

The output of Program A and B are exactly the same, but program B will run not only faster, but it takes up less space and uses less processing power.
Now, onto the choice of a compiler; the compiler is the tool all of us coders need in order to create our program. Of course you all know that, but sometimes the compiler we chose for a certain project may not be the best. Lets take a look back at Program A and Program B now Compiled in Microsoft Visual C++ 6.0 Professional Edition. Again, the output is the same, but this time, the file sizes have leaped to take an incredible amount of space. Program A now takes up 200KB and Program B takes up 168 KB. Now, I’m not saying that any compilers are bad; I’m simply saying that some projects would best be compiled in different compilers.
Now onto functions… Some headers are called too much which contain simple functions like strlen(). What this does is it slows the program down even though you only want to use strlen()… Yet, there is an alternative. Why not make your own strlen()? Yes, and it’s totally possible too.

unsigned int strlen (const char string[]) {
int i=0;
while(string[i]!='\x0') ++i;
return i;
}

Ok, the last part here is a little difficult so if you don’t get it it’s ok because this will be better explained in a follow up tutorial. Let us say that O(N), N being a dataset measures the efficiency of an algorithm and O being the worse case scenario. O(x) is a function describing the worst-case complexity of an algorithm. If you run through your dataset, each iteration you run through again is O(N^2). For instance, if your dataset is already sorted, running a quicksort on it will be an O(N^2) running time. A binary search is O(log N) because each iteration you halve the size of your search space. (Special thanks to Evil_Enchilada for help on this section).

for(int i = 0; i < N; i++) {
...
}
= O(N)
Loop once through each element

for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
...
}
}
= O(N^2)

for(int i = 0; i < N; i *= 2) {
...
}
= O(log N) **Keep in mind that Log of N is in base 2**

Note that O(log N) is a more efficient algorithm than O(N)

For more information on Big O Notation go to http://leepoint.net/notes/cpp/algorithms/bigoh.html