Monthly Read Posts in Dec 2016
C++ Performance: Common Wisdoms and Common “Wisdoms”
The post discusses some oftenly-argued performance related aspects of C++.
- C++ Exceptions – (Almost-)Zero-Cost until Thrown
- Smart Pointers and STL: Allocations are Still Evil
- Using boost:: – Case by Case and Only If You Do Need It
- Polymorphism – Two-Fold Impact
- RTTI – Can be Easily Avoided Most of the Time
- On Branch (mis)-Predictions and Pipeline Stalls
- On inline asm and SSE
- On inlines and force-inlines
- On templates
- On compiler flags
- On Manual Loop Unrolling
- “For-free” Optimizations a.k.a. Avoiding Premature Pessimization
All suggestion based on a fundamental pre-requisite: Three things Really Matter for performance.
The first one is Algorithm, the second one is your code being Non-Blocking, and the third one is Data Locality.
The rest of the tricks, while potentially useful, should be used only on case-by-case basis, when a bottleneck is identified. Otherwise it will be a Premature Optimisation.
Iterables vs. Iterators vs. Generators
Anything that can return an iterator (usually via overriding
__iter__()) is iterable.
Anything that can be iterated (usually via overriding
__next__(), or overriding
next() in python 2.x) is an iterator.
A generator is a special kind of iterator; it make you avoid writing
Two types of generator:
- Generator function: A function with yield to return value, the return value of the functio call is a generator
- Generator expression: A expression using comprehension-syntax
Note：有 C++ STL 背景基本看这种问题都秒懂…
Competing constructor: two constructors that would take the same sequence of arguments (if one is somehow made invisible), but also that they do different, incompatible things.
- Competing functions complicate resolution for human beings
- Don’t write competing constructors, or even competing functions.
- Take care of class template function, one instantiation may turn into a competing function.
- Use tag with overload function can benefit from more clearer semantics