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.
News
The State of Chatbots: Pandemic Edition
Jessica Davis, Senior Editor, Enterprise Apps,  9/10/2020
Commentary
Deloitte on Cloud, the Edge, and Enterprise Expectations
Joao-Pierre S. Ruth, Senior Writer,  9/14/2020
Slideshows
Data Science: How the Pandemic Has Affected 10 Popular Jobs
Cynthia Harvey, Freelance Journalist, InformationWeek,  9/9/2020
White Papers
Register for InformationWeek Newsletters
2020 State of DevOps Report
2020 State of DevOps Report
Download this report today to learn more about the key tools and technologies being utilized, and how organizations deal with the cultural and process changes that DevOps brings. The report also examines the barriers organizations face, as well as the rewards from DevOps including faster application delivery, higher quality products, and quicker recovery from errors in production.
Video
Current Issue
IT Automation Transforms Network Management
In this special report we will examine the layers of automation and orchestration in IT operations, and how they can provide high availability and greater scale for modern applications and business demands.
Slideshows
Flash Poll