Skip to content

Commit

Permalink
Add note to 'never use std::bind'
Browse files Browse the repository at this point in the history
  • Loading branch information
lefticus committed May 25, 2016
1 parent 4ce45d6 commit 251eb8f
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 08-Considering_Performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
```

0 comments on commit 251eb8f

Please sign in to comment.