From 8baa4589e3f409dda89b6991ec4850b1879df797 Mon Sep 17 00:00:00 2001 From: Damian Gryski Date: Fri, 9 Mar 2018 08:55:05 -0800 Subject: [PATCH] mumblings on branch prediction --- performance.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/performance.md b/performance.md index 005935c..b94a104 100644 --- a/performance.md +++ b/performance.md @@ -859,6 +859,22 @@ Techniques specific to the architecture running the code * reducing pointer chasing * temporal and spacial locality: use what you have and what's nearby as much as possible * branch prediction + * remove branches from inner loops: + if a { for { } } else { for { } } + instead of + for { if a { } else { } } + benchmark due to branch prediction + structure to avoid branch + + if i % 2 == 0 { + evens++ + } else { + odds++ + } + + counts[i & 1] ++ + "branch-free code", benchmark; not always faster, but frequently harder to read + * function call overhead * reduce data copies