{"id":16242585,"url":"https://github.com/facontidavide/cpp_optimizations_diary","last_synced_at":"2025-05-16T06:05:07.166Z","repository":{"id":39657157,"uuid":"261566073","full_name":"facontidavide/CPP_Optimizations_Diary","owner":"facontidavide","description":"Tips and tricks to optimize your C++ code","archived":false,"fork":false,"pushed_at":"2023-12-21T03:01:11.000Z","size":13953,"stargazers_count":1219,"open_issues_count":2,"forks_count":85,"subscribers_count":90,"default_branch":"master","last_synced_at":"2025-05-01T09:38:58.073Z","etag":null,"topics":["blog","cpp","optimization"],"latest_commit_sha":null,"homepage":"https://cpp-optimizations.netlify.app/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/facontidavide.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"facontidavide","custom":"https://www.paypal.me/facontidavide"}},"created_at":"2020-05-05T19:35:41.000Z","updated_at":"2025-04-27T08:12:27.000Z","dependencies_parsed_at":"2024-10-25T23:13:50.140Z","dependency_job_id":null,"html_url":"https://github.com/facontidavide/CPP_Optimizations_Diary","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/facontidavide%2FCPP_Optimizations_Diary","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/facontidavide%2FCPP_Optimizations_Diary/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/facontidavide%2FCPP_Optimizations_Diary/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/facontidavide%2FCPP_Optimizations_Diary/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/facontidavide","download_url":"https://codeload.github.com/facontidavide/CPP_Optimizations_Diary/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254478187,"owners_count":22077676,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blog","cpp","optimization"],"created_at":"2024-10-10T14:11:39.731Z","updated_at":"2025-05-16T06:05:07.149Z","avatar_url":"https://github.com/facontidavide.png","language":"C++","readme":"# Preface: about me\n\n My name is [Davide Faconti](https://twitter.com/facontidavide) and my job is one of the best in the world: I work in **robotics**.\n  \nThis 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*.\n\nI love C++ programming and Open Source and this \"diary\" is my small contribution to the OSS community.\n\n# CPP Optimizations Diary\n\nOptimizing code in C++ is something that no one can resist. You can have fun\nand pretend that you are doing something useful for your organization at the same time!\n\nIn this repository, I will record some simple design patterns to improve your code \nand remove unnecessary overhead in **C++**.\n\nIf you are a seasoned C++ expert, you probably have your own set of rules already.\n\nThese rules help you look like a bad-ass/rockstar/10X engineer to your colleagues.\n\nYou are the kind of person that casually drops a [std::vector\u003c\u003e::reserve](docs/reserve.md) before a loop and\nnods, smiling, looking at the performance improvement and the astonishment of your team member.\n\n\u003cp align=\"center\"\u003e\u003cimg src=\"docs/img/boom.gif\" width=\"350\"\u003e\u003c/p\u003e\n\n\nHopefully, the examples in this repository will help you achieve this status of guru\nand, as a side effect, save the planet from global warming, sparing useless CPU\ncycles from being wasted.\n\nThen, unfortunately, someone on the other side of the planet will start mining Bitcoins or write her/his \napplication in **Python** and all your effort to save electricity was for nothing.\n\nI am kidding, Python developers, we love you!\n\n\u003e Narrator: \"he was not kidding...\"\n\n## Rule 1: measure first (using _good_ tools)\n\nThe very first thing any person concerned about performance should do is:\n \n- **Measure first** and **make hypothesis later**.\n\nMe and my colleagues are almost always wrong about the reasons a piece of code is slow. \n\nSometimes we are right, but it is really hard to know in advance how refactoring will\nimprove performance. Good profiling tools show in minutes the \"low hanging fruits\": minimum work, maximum benefit!\n\nSummarizing: 10 minutes profiling can save you hours of guessing and refactoring.\n\nMy \"goto\" tools in Linux are [Hotspot](https://github.com/KDAB/hotspot) and \n[Heaptrack](https://github.com/KDE/heaptrack). I understand Windows has similar\ntools too.\n\n\u003cp align=\"center\"\u003e\u003cimg src=\"docs/img/hotspot_heaptrack.jpg\" width=\"350\"\u003e\u003c/p\u003e\n\nIn the benchmark war, if you are the soldier, these are your rifle and hand grenades.\n\nOnce you know which part of the code deserves to be optimized, you might want to use\n[Google Benchmark](https://github.com/google/benchmark) to measure the time spent in a very specific\nclass or function.\n\nYou can even run it Google Benchmark online here: [quick-bench.com](http://quick-bench.com/G7B2w0xPUWgOVvuzI7unES6cU4w).\n\n![quick-bench](docs/img/quick-bench.png)\n\n## Rule 2: learn good design patterns, use them by default\n\nWriting good code is like brushing your teeth: you should do it without thinking too much about it.\n\nIt is a muscle that you need to train, that will become stronger over time. But don't worry:\nonce you start, you will start seeing recurring patterns that \nare surprisingly simple and works in many different use cases.\n\n**Spoiler alert**: one of my most beloved tricks is to _minimize the number of heap allocations_.\nYou have no idea how much that helps.\n\nBut let's make something absolutely clear: \n\n- Your **first goal** as a developer (software engineer?) is to create code that is **correct** and fulfil the requirements.\n- The **second** most important thing is to make your code **maintainable and readable** for other people.\n- In many cases, you also want to make code faster, because [faster code is better code](https://craigmod.com/essays/fast_software/).\n\nIn other words, think twice before doing any change in your code that makes it less readable or harder to debug,\njust because you believe it may run 2.5% faster.\n\n# Get started\n\nFor a more comfortable reading experience, visit: https://cpp-optimizations.netlify.app\n\n## Optimization examples\n\n### \"If you pass that by value one more time...\"\n\n- [Use Const reference by default](docs/prefer_references.md).\n\n- Move semantic (TODO).\n\n- Return value optimization (TODO).\n\n\n### std::vector\u003c\u003e is your best friend\n\n\n- [Use std::vector\u003c\u003e::reserve by default](docs/reserve.md)\n\n- [\"I have learnt linked-lists at university, should I use them?\" Nooope](docs/no_lists.md).\n\n- [You don't need a `std::map\u003c\u003e` for that](docs/dont_need_map.md).\n\n- [Small vector optimization](docs/small_vectors.md)\n\n\n### \"It is just a string, how bad could that be?\"\n\n- [Strings are (almost) vectors](docs/strings_are_vectors.md)\n\n- [When not to worry: small string optimization](docs/small_strings.md).\n\n- [String concatenation: the false sense of security of `operator+`](docs/strings_concatenation.md).\n\n- `std::string_view`: love at first sight (TODO).\n\n### Don't compute things twice.\n\n- [Example: 2D/3D transforms the right way](docs/2d_transforms.md).\n\n- [Iterating over a 2D matrix: less elegant, more performant](docs/2d_matrix_iteration.md).\n\n### Fantastic data structures and where to find them.\n\n- [I tried `boost::container::flat_map`. You won't imagine what happened next](docs/boost_flatmap.md).\n\n### Case studies\n\n- [Simpler and faster way to filter Point Clouds in PCL.](docs/pcl_filter.md)\n\n- [Fast Palindrome: the cost of conditional branches](docs/palindrome.md)\n\n\n# License\n\n\u003cp xmlns:dct=\"http://purl.org/dc/terms/\" xmlns:cc=\"http://creativecommons.org/ns#\" class=\"license-text\"\u003eThis work   is licensed under \u003ca rel=\"license\" href=\"https://creativecommons.org/licenses/by-sa/4.0\"\u003eCC BY-SA 4.0\u003cimg style=\"height:22px!important;margin-left:3px;vertical-align:text-bottom;\" src=\"https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1\" /\u003e\u003cimg style=\"height:22px!important;margin-left:3px;vertical-align:text-bottom;\" src=\"https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1\" /\u003e\u003cimg style=\"height:22px!important;margin-left:3px;vertical-align:text-bottom;\" src=\"https://mirrors.creativecommons.org/presskit/icons/sa.svg?ref=chooser-v1\" /\u003e\u003c/a\u003e\u003c/p\u003e\n","funding_links":["https://github.com/sponsors/facontidavide","https://www.paypal.me/facontidavide"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffacontidavide%2Fcpp_optimizations_diary","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffacontidavide%2Fcpp_optimizations_diary","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffacontidavide%2Fcpp_optimizations_diary/lists"}