• Stars
    star
    964
  • Rank 47,452 (Top 1.0 %)
  • Language
    C++
  • Created over 4 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Tips and tricks to optimize your C++ code

Preface: about me

My name is Davide Faconti and my job is one of the best in the world: I work in robotics.

This blog/repository is maintained in my spare time and it is not related to my work there. Therefore opinions (and memes) are all mine and don't represent my employer in any way.

I love C++ programming and Open Source and this "diary" is my small contribution to the OSS community.

CPP Optimizations Diary

Optimizing code in C++ is something that no one can resist. You can have fun and pretend that you are doing something useful for your organization at the same time!

In this repository, I will record some simple design patterns to improve your code and remove unnecessary overhead in C++.

If you are a seasoned C++ expert, you probably have your own set of rules already.

These rules help you look like a bad-ass/rockstar/10X engineer to your colleagues.

You are the kind of person that casually drops a std::vector<>::reserve before a loop and nods, smiling, looking at the performance improvement and the astonishment of your team member.

Hopefully, the examples in this repository will help you achieve this status of guru and, as a side effect, save the planet from global warming, sparing useless CPU cycles from being wasted.

Then, unfortunately, someone on the other side of the planet will start mining Bitcoins or write her/his application in Python and all your effort to save electricity was for nothing.

I am kidding, Python developers, we love you!

Narrator: "he was not kidding..."

Rule 1: measure first (using good tools)

The very first thing any person concerned about performance should do is:

  • Measure first and make hypothesis later.

Me and my colleagues are almost always wrong about the reasons a piece of code is slow.

Sometimes we are right, but it is really hard to know in advance how refactoring will improve performance. Good profiling tools show in minutes the "low hanging fruits": minimum work, maximum benefit!

Summarizing: 10 minutes profiling can save you hours of guessing and refactoring.

My "goto" tools in Linux are Hotspot and Heaptrack. I understand Windows has similar tools too.

In the benchmark war, if you are the soldier, these are your rifle and hand grenades.

Once you know which part of the code deserves to be optimized, you might want to use Google Benchmark to measure the time spent in a very specific class or function.

You can even run it Google Benchmark online here: quick-bench.com.

quick-bench

Rule 2: learn good design patterns, use them by default

Writing good code is like brushing your teeth: you should do it without thinking too much about it.

It is a muscle that you need to train, that will become stronger over time. But don't worry: once you start, you will start seeing recurring patterns that are surprisingly simple and works in many different use cases.

Spoiler alert: one of my most beloved tricks is to minimize the number of heap allocations. You have no idea how much that helps.

But let's make something absolutely clear:

  • Your first goal as a developer (software engineer?) is to create code that is correct and fulfil the requirements.
  • The second most important thing is to make your code maintainable and readable for other people.
  • In many cases, you also want to make code faster, because faster code is better code.

In other words, think twice before doing any change in your code that makes it less readable or harder to debug, just because you believe it may run 2.5% faster.

Get started

For a more comfortable reading experience, visit: https://cpp-optimizations.netlify.app

Optimization examples

"If you pass that by value one more time..."

std::vector<> is your best friend

"It is just a string, how bad could that be?"

Don't compute things twice.

Fantastic data structures and where to find them.

Case studies

License

This work is licensed under CC BY-SA 4.0

More Repositories

1

PlotJuggler

The Time Series Visualization Tool that you deserve.
C++
3,742
star
2

Bonxai

Fast, hierarchical, sparse Voxel Grid
C++
513
star
3

rosbag_editor

Create a rosbag from a given one, using a simple GUI
C++
357
star
4

ros_msg_parser

Deserialized any ROS messages, even if you don't know its type. Successor of ros_type_introspection.
C++
79
star
5

ros_type_introspection

Deserialize ROS messages that are unknown at compilation time
C++
59
star
6

cpp_swiss_army_knife

A collection of nice and tiny C++ libraries
38
star
7

PointCloudProcessing

C++
29
star
8

ros2_logging_fmt

C++
25
star
9

ros-docker-gazebo

Shell
24
star
10

ros2_introspection

C++
18
star
11

rosx_introspection

Deserialize any ROS message, without compilation time information.
C++
12
star
12

type_introspection_tests

Tests and examples related to https://github.com/facontidavide/ros_type_introspection
C++
11
star
13

PluginLoader

C++ plugin manager based on https://github.com/ros/class_loader
C++
8
star
14

SerializeMe

C++ helper functions to deserialize data from a buffer
C++
8
star
15

ProtectedValue

C++
7
star
16

SafeCppAny

Extension of std::any with small object optmization and safe numeric conversion.
C++
6
star
17

rqt_console_plus

Alternative implementation of rqt_console for ROS
C++
5
star
18

CanMove

CANOpen master library.
C++
5
star
19

ros_msg_parser-release

C++
4
star
20

ROS_msg_to_PCL

C++
4
star
21

CppUnits11

Implementation of type safe units in C++11
C++
2
star
22

mcap_editor

2
star
23

AsyncSignals

Proof of concept implementation of signals pushed on a separate event loop
C++
2
star
24

plotjuggler-release

2
star
25

cost_map_core

Code extracted from https://github.com/stonier/cost_map
C++
2
star
26

QtTreeEditor

1
star
27

btcpp_sample_project

1
star
28

Meanshift

Implementation of MeanShift algorithm (including Qt Demo)
C++
1
star
29

QtChaiScript

QT front-end for ChaiScript
C++
1
star
30

BOR_laser_test

Programming test. Convert LaserScan
1
star
31

PCL_VoxelGrid_enhanced

C++
1
star
32

Blackboard

WIP implementation of a blackboard. To be used with Behavior-Tree
C++
1
star
33

Barcelona-Cpp-Meetups

1
star
34

facontidavide

1
star
35

StringTree

C++
1
star
36

rosbag_editor-release

1
star
37

TimeseriesLogger

WORK IN PROGRESS. C++ header-only library to log numerical values. Low overhead and low latency guaranteed.
C++
1
star