flames

C Code That Works < C Code That Rocks

Many beginners and even some experienced developers believe that writing code that works is all that matters. This statement may be partially true, but isn’t it better to deliver optimized code that does what it has to in less time, with less required resources, looks cleaner and performs better?
Let’s talk about C and C++. You can either write code that works but looks average or you may create fully operational functionality with enhanced security (less flaws are present), improved performance (speed of operations) and decreased consumption of resources (memory). Sure, I know that no one can write perfect software, but that does not mean we should not try, right guys?

Ready to give it a shot? Here are several primary guidelines:

  • You will never achieve full performance optimization. Perfection here is as unreachable as in any other field of programming. Be prepared to face the fact your code will never be completely, 100% optimized. And there will always be enough place to implement new improvements.
  • Optimization is not a linear process. Your goals may differ and they, sometimes, even conflict with each other. For example elimination of some potential security breaches may dramatically affect performance and vice versa.
  • There are lots of tools, techniques and practices available, but… Some are better than others in particular tasks, and may potentially harm some of your optimization processes like, for example, generation of decently larger files and/or memory footprints.
  • You can always use some dirty tricks, but should you? Yes, there are multiple tricks and hacks that allow your code to run at the speed of light but you will be forced to break several standards of coding. Would it be worth it?

Now, that new ground is covered, we may proceed to some more practical pieces of advice that will hopefully assist you with C/C++ development and code optimization.

Operators

Basic operators exist for a good reason and deliver numerous development benefits. However things are not perfect with them as -=,*= and += can dramatically slow down your entire program when you choose to apply them to some basic data types. However you will only be sure after all is nice and comfy inside the assembler on your machine.
Possible solutions that allow optimization here differ. Here are some of them:

  • The good: try using prefix versions of decrement and increment instead of postfix ones. One way or the other you will, at least, have some fun. And, despite how the idea sounds it often works out splendid.
  • The bad: division and multiplication operators can be replaced with >>or<< to achieve a significant boost, however this may is tricky and mischievous. You can easily end up in real trouble with a mistake that can be fixed with addition of several range estimations, but such a fix will leave you with code that performs worse than before you have even touched it.
  • The ugly: Bit operator tricks you probably are fully aware of can indeed skyrocket speed program speed however you may easily end up with a solution that’s 100% machine-dependent. You don’t want that. Protect yourselves with add move via your C++ assembler.

 

A complete C code optimization guide that is somewhat close to full coverage will take quite more than one post and we all realize that. That is why I have decided to split this work into several consumable chunks. Please stay tuned for all following articles. We will be covering Loop and Binary Search optimization shortly.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

    Tagged: ,


    Facebook Comments

    comments

    'C Code That Works < C Code That Rocks' have no comments

    Be the first to comment this post!

    Would you like to share your thoughts?

    Your email address will not be published.

    Software development and outsourcing blog by QArea © 2016

    Яндекс.Метрика