##10c++coding habits to optimize perf

Many of these suggestions are based on [[optimized c++]]

· #1 Habit – in c++ at least, ++counter performance is strictly “better or equal to” counter++. If there’s no compelling reason, I would prefer the former.

· #2 Habit – in a for loop, one of the beginning and ending values is more expensive to evaluate. Choose the more expensive one as the beginning value, so you don’t evaluate it over and over. Some people object that compiler can cache the more expensive end value, but 2016 tests show otherwise.

· If a method can be static, then make it static. Good for performance and semantics.

· For a small if-else-if block, put the most likely scenario first. May affect readability. Worthwhile only in a hot spot.

· For a long if-elif-elif-elif-elif block, a switch statement performance is strictly “greater or equal”

· For-loop starts by checking the condition (2nd component in header). If this initial check is redundant (as often is), then use a do-while loop

· Call a loop in a function, rather than call a function in a loop. Another micro-optimization.

Advertisements

##boost modules USED ]finance n asked]IV

#1) shared_ptr (+ intrusive_ptr) — I feel more than half  (70%?) of the “finance” boost usage is here. I feel every architect who chooses boost will use shared_ptr
#2) boost thread
# serialization
# boost::ANY

Gregory said bbg used shared_ptr, hash tables and function binders from boost

Overall, I feel these topics are mostly QQ types, frequently asked in IV (esp. those in bold) but not central to the language. I would put only smart pointers in my Tier 1 or Tier 2.

—— other modules used in my systems
* noncopyable — for singletons
** private derivation
** prevents synthesized {op= and copier}, since base class is noncopyable.

* polymorphic_cast
* numeric_cast — mostly int types, also float types

* operators ?
* bind?
* tuple
* regex
* scoped_ptr — as non-copyable [1] stackVar,
[1] different from auto_ptr

[[linux programmer’s toolbox]]

MALLOC_CHECK_ is a glibc env var
–debugger on optimized code
P558 Sometimes without compiler optimization performance is unacceptable.

To prevent optimizer removing your variables, mark them volatile.

An inline function may not appear in call stack. Consider “-fno-inline”

–P569 double-free may not show issues until the next free() or malloc()

–P470 – 472 sar command 
can show per-process performance data

can monitor network devices

—P515 printf + macros for debugging

buffering behavior differs between terminal ^ log files

## loaded words in c++ literature

“safety” means exception …
“resource” is always represented by an object on heap (accessed via a pointer).
“life cycle” means destructor
“owner” means deleter of a heapy thingy. Non-heap objects need no owner. Owner has just one job — deletion.

“static” is often used without qualification and by default means the most common/popular static object — the locals
“functor” often includes function pointer, at least in some contexts
The “resource” in RAII means any memory allocated from the heap

## c++11 features I understand as significant

C++11 features I understand as significant. Some significant c++11 features I don't apprecaite. I will also skip TR1 features.

#1 thread library

http://solarianprogrammer.com/2011/12/16/cpp-11-thread-tutorial/

#2
hashed containers

http://stackoverflow.com/questions/7176157/is-there-a-standard-c-hash-container

http://en.wikipedia.org/wiki/Unordered_associative_containers_(C%2B%2B) — implemented in both SGI and boost

#3 unique ptr

# lambda

# initializer syntax