C++ Code Optimization: 10 Tips And Tricks - InformationWeek

InformationWeek is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them.Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

C++ Code Optimization: 10 Tips And Tricks
Newest First  |  Oldest First  |  Threaded View
User Rank: Apprentice
11/18/2015 | 1:18:32 PM
Some of these tips are outdated in modern C++
As appropiate as many of those tips may have been some time in the past, C++ and its compilers have evolved in the last decades. In fact, I consider some of these tips to be harmful in modern C++, since there are better alternatives. I'll address each tip separately
  1. use delete[] for cleanup: Please don't. C++ has smart pointers in the standard library for half a decade now, and had them in libraries like boost and the TR1 addition for more than half a decade before that. In modern C++, plain new and delete is a code smell, unless you are implementing a smart pointer, container or similar library class. Replace that tip with "use RAII classes". Also: "prefer stack allocation over heap allocation, if applicable."
  2. Minimize temporary objects: Return value optimization and named return value optimization have been around for a looooong time now. A very long time. If you use a compiler that has been released this century, it almost certainly implements those optimizations, so there are no temporary object except in debug builds. In addition, a = b + c is clearer to read which is much more valuable than an attempt at an optimization that does not exist in a piece of code that maybe does not even need it. My replacement tip: "Don't prematurely optimize, use a profiler instead." And, of course, "initialize variables right when you define them, don't define them until you use them." -->  Complex a = b + c;
  3. I agree in general. However, you suggest to first use arrays, then to get comfortable with containers. Containers should be among the first things to get comfortable with, preferably even before learning about raw allocation for arrays (see above). std::vector is a good replacement for C arrays, with little to no overhead.
  4. = != ==: modern compilers have some builtin static analysis, and dedicated static analyzers should be part of every serious build chain. If you use assignment instead of comparison e.g. in conditionals, compilers usually emit warnings, as they do if you do not use the return of a comparison because you meant a assignment. So, instead of being pedantic about "=" symbols use a modern compiler and static analysis to defend against this and many more common errors.
  5. No globals is a good policy. Even if you need communications between functions - prefer passing that communication explicitly via arguments rather than implicitly via globals.
  6. Nothing to add - except that static analyzers often catch misused postfix increment/decrement.
  7. Replace "makefile" with "build tool" here - there are lots of them, and many have an less clumsy syntax and are easier to use than make. Apart from that, I definitely agree.
  8. Yes, compile on different compilers.
  9. Flowcharts: I generally agree, although it sounds a bit low level. Most low level flow should be obvious from the code (refactor if it isn't). For complex high level stuff class diagrams, data flow diagrams etc are a useful tool.
  10. Yes, definitely.

I already added some "replacement tips" that I consider to be worthy of the top ten. One I am missing is "use automated tests". The fastest, safest code is worth nothing if it is wrong.

User Rank: Apprentice
11/16/2015 | 11:00:12 AM
Thanks for the Splendid list of C++ Optimization Tips.
All the tips are amazing and effective to be used in production development environment. Thanks for coming up with such a great post.

State of the Cloud
State of the Cloud
Cloud has drastically changed how IT organizations consume and deploy services in the digital age. This research report will delve into public, private and hybrid cloud adoption trends, with a special focus on infrastructure as a service and its role in the enterprise. Find out the challenges organizations are experiencing, and the technologies and strategies they are using to manage and mitigate those challenges today.
IT Careers: Top 10 US Cities for Tech Jobs
Cynthia Harvey, Freelance Journalist, InformationWeek,  1/14/2020
Predictions for Cloud Computing in 2020
James Kobielus, Research Director, Futurum,  1/9/2020
What's Next: AI and Data Trends for 2020 and Beyond
Jessica Davis, Senior Editor, Enterprise Apps,  12/30/2019
Register for InformationWeek Newsletters
Current Issue
The Cloud Gets Ready for the 20's
This IT Trend Report explores how cloud computing is being shaped for the next phase in its maturation. It will help enterprise IT decision makers and business leaders understand some of the key trends reflected emerging cloud concepts and technologies, and in enterprise cloud usage patterns. Get it today!
White Papers
Twitter Feed
Sponsored Live Streaming Video
Everything You've Been Told About Mobility Is Wrong
Attend this video symposium with Sean Wisdom, Global Director of Mobility Solutions, and learn about how you can harness powerful new products to mobilize your business potential.
Sponsored Video
Flash Poll