Wednesday, September 23, 2015

if-elseif Statement: Most common conditions shall be handled first

·         Discuss the overhead of branching and why the most common case should be tested first in the (if – else) if construct to speed up execution.
o   Modern processors use pipelining.
From Wikipedia
o   This means that multiple commands are served in parallel

o   Ex:  in clock cycle 4
§  Cmd1: Fetch from memory
§  Cmd 2: Decode to the ALU
§  Cmd 3: Execute
§  Cmd 4: Write the result to the register or the memory
o   This requires that the CPU know the next commands to execute
o   If we have conditional branches, then, the CPU will not be able to know the next commands, and then it will not be able to utilize the pipelining.

o   Hence, it is recommended that the most common condition be the first condition in if-else statement, so we do not have a lot of conditional branching.

No comments:

Post a Comment