You need to consider the complexity of your branches, the compiler can delete branches using architecture-specific operation codes, such as CMOV (comparison and movement).
Simple code example
if (b > 15) count += (b+10);
Here is the code compiled into machine language
;; assembly x86 FASM/NASM syntax ;; WITH branching MOV ebx, [b] ;; b MOV ecx, [count] ;; count CMP ebx, 15 ;; if condition to set flags JLE .skip ;; { branch/jump over the if body when less than or equal LEA eax, [ecx + ebx + 10] ;; count + b+10 MOV [count], eax ;; store count .skip: ;; } label after the if block ;; WITHOUT branching MOV ebx, [b] ;; b MOV ecx, [count] ;; count LEA eax, [ecx + ebx + 10] ;; pre-calc avoiding the need to branch CMP ebx, 15 ;; if condition to set flags CMOVLE eax, ecx ;; make eax equal to ecx (current count) when less than or equal ;; avoiding the branch/jump MOV [count], eax ;; store count
So, if you donβt know how the optimizing compiler optimizes your code, itβs a little difficult to profile branch prediction. If you check the output of your machine code and know that you have many J [condition] statements, then itβs enough to use the code profiling tool mentioned in the comments. Trying to overturn your own branch prediction test without using the proper debug register architecture will lead to the situation I demonstrated above.
source share