Results 1 to 4 of 4

Thread: Efficient C++ Programming

  1. #1
    Senior Member
    Join Date
    Jan 2002

    Post Efficient C++ Programming

    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

  2. #2
    Senior Member
    Join Date
    Sep 2001

    I think your effort is well meant, but I don't quite agree with your 3 first points:
    1- Try printf ing a String (String object)... How often in C++ will you *only* output hard coded fixed lenght strings (fixed lenght null terminated strings). If you use C++ you probably intend to use the String class at somepoint.. As soone as you use it once in your prog, your build time and size advantage is out the window... Now perhaps if we were talking exec time but then again that might be arguable..

    2- Compilers: true some compilers optimize better than others, but in your comparaison, what options did you use in VC++? Was it even a release build? (Perhaps it was if so overlook my comment, but you should provide more details of your "benchmark" for this to be considered seriously)

    3- Redefining well established functions is just gonna bring trouble to save a few bytes of build output... This is not to mention code maintainability which is my next point...

    Code optimisation is great, but you shouldn't overlook the maintainability of the code to gain a few microseconds of build or exec time or a few bytes of build output. Don't get me wrong, I don't like code bloat, but in today's software industry, something like 85% percent of the task is maintaining code. IMHO, it's not worth doing some obscure optimisation trick if it'll take you 10 hours later just re-reading your own code to figure out why it's done that way and how to debug it...

    Positive comment: The big O stuff IS important to understand as you can easily blow off big time cpu cycles needlessly on algos or data structures that are not adapted to the problem...

    My 2 cents...


    PS: Once again, I'm all for optimisation, but only in places that it will really change something, like in a loop that's gonna execute millions of times per second.. not in a prompt that will take away .00002 seconds of a human user's time...
    Credit travels up, blame travels down -- The Boss

  3. #3
    Senior Member
    Join Date
    Jan 2002

    Additional Links

    Here are some links about C++ Optimization which can be useful until my follow up is written.


    http://www.amazon.com/exec/obidos/IS...163089-0911163 <A Good Book to Buy>


  4. #4
    good tut gamemaster and ammo.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts