From 251eb8fa3fb8abf416ab6050c595ad0af5b8698c Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 25 May 2016 15:28:39 -0600 Subject: [PATCH] Add note to 'never use std::bind' --- 08-Considering_Performance.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/08-Considering_Performance.md b/08-Considering_Performance.md index 903cfe5..4d0fab2 100644 --- a/08-Considering_Performance.md +++ b/08-Considering_Performance.md @@ -307,3 +307,18 @@ std::cout << someThing() << '\n'; This is very minor, but a `"\n"` has to be parsed by the compiler as a `const char *` which has to do a range check for `\0` when writing it to the stream (or appending to a string). A '\n' is known to be a single character and avoids many CPU instructions. If used inefficiently very many times it might have an impact on your performance, but more importantly thinking about these two usage cases gets you thinking more about what the compiler and runtime has to do to execute your code. + + +### Never Use `std::bind` + +`std::bind` is almost always way more overhead (both compile time and runtime) than you need. Instead simply use a lambda. + +```cpp +// Bad Idea +auto f = std::bind(&my_function, "hello", std::placeholders::_1); +f("world"); + +// Good Idea +auto f = [](const std::string &s) { return my_function("hello", s); }; +f("world") +```