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.

IoT
IoT
Software
News
11/15/2015
12:06 PM

C++ Code Optimization: 10 Tips And Tricks

C++ is a great enterprise programming language if you know how to get the most from its arcane features. Here are 10 tips to help you do so, and end up with high-performing applications for your business.
2 of 11

Reduce, Reuse, Recycle
One of the problems that can hit any platform is an application that grabs the memory it needs, grabs more memory as it goes along, and never lets any memory go. It doesn't have to be a runaway process that chews through all the available memory and more. It can be an otherwise lovely app that doesn't effectively release the memory it requires.
The key to effectively letting go of memory is to use 'delete []' when you're through with arrays (or their members), functions, and pointers. In general you'll want to make sure that you include the delete [] to clean up after every local function. Your users will be glad you did.
(Image: OpenClipartVectors via Pixabay)

Reduce, Reuse, Recycle

One of the problems that can hit any platform is an application that grabs the memory it needs, grabs more memory as it goes along, and never lets any memory go. It doesn't have to be a runaway process that chews through all the available memory and more. It can be an otherwise lovely app that doesn't effectively release the memory it requires.

The key to effectively letting go of memory is to use "delete []" when you're through with arrays (or their members), functions, and pointers. In general you'll want to make sure that you include the delete [] to clean up after every local function. Your users will be glad you did.

(Image: OpenClipartVectors via Pixabay)

2 of 11
Comment  | 
Print  | 
Comments
Newest First  |  Oldest First  |  Threaded View
arne_mertz
100%
0%
arne_mertz,
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.

 
TechBeamers
100%
0%
TechBeamers,
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.
Commentary
Get Your Enterprise Ready for 5G
Mary E. Shacklett, Mary E. Shacklett,  1/14/2020
Commentary
Modern App Dev: An Enterprise Guide
Cathleen Gagne, Managing Editor, InformationWeek,  1/5/2020
Slideshows
9 Ways to Improve IT and Operational Efficiencies in 2020
Cynthia Harvey, Freelance Journalist, InformationWeek,  1/2/2020
White Papers
Register for InformationWeek Newsletters
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.
Video
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!
Slideshows
Flash Poll