Coder Social home page Coder Social logo

cpp_optimizations_diary's Introduction

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 already a set of rules in your head that you always follow.

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 perfomance 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 warning, sparing useless CPU cycles from being wasted.

Then, of course, 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 perfomance 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 be slow.

Sometimes we are right, but it is really hard to know in advance ho 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 work 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 granades.

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 I am getting ahead of myself...

Optimization examples

"If you pass that by value one more time, I swear to god that..."

TODO

  • Const reference by default.

  • Move semantic and return value optimization.

std::vector<>. Your best friend

TODO

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

TODO

  • When not to worry: small string optimization.

  • String concatenation: the false sense of security of std::string::operator+.

  • std::string_view: love at first sight.

Don't compute that twice; practical examples of caching.

TODO

  • Example: 2D/3D transforms the smart way.

  • Double for-loop: less elegant, more performant version.

Fantastic data structures and where to find them.

TODO

cpp_optimizations_diary's People

Contributors

facontidavide avatar imgbotapp avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.