Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execution speed of conditional instructions compared to mathematical functions [closed]

Tags:

I will try to be as clear as I can with my question (not easy...it's not that clear for me as well). Suppose you have a set of IF...THEN instructions with multiple operands, for example

IF ((a==0) && (b==1) && (c==1)) THEN x=1
ELSE IF ((a==0) && (b==0) && (c==1)) THEN x=2-

and so on

Suppose I could replace all those IFs with a single mathematical function like x = a * n1 + b * n2 + c * n3 (this is just to give you an idea, in real it's more complex, but also the IFs and operands are many more)

The function comes from a previously trained artificial neural network.

My gut feeling is that when it comes to execution, the function should take lots less time than the IFs, but it's just a gut feeling that comes from my old background in assembly where they taught us that a conditional instruction takes way more time than an arithmetical one.

Can you confirm this? maybe even provide me some link where I could find an explanation?

Thank you in advance guys!

like image 643
Maxyone Avatar asked Apr 06 '16 11:04

Maxyone


1 Answers

Your gut feeling is correct.

The problem is that modern processors have a pipeline, and in the pipeline the next x instructions are loaded sequentially ready for execution. If you have a branch, an if statement, then the processor doesn't know which code path you're going to be taking next, and so it guesses using the branch predictor, but if it gets it wrong it has to throw out all of the pipeline and start again down the correct branch.

Branch predictors in modern processors are quite good, but if you have something that has a 50/50 chances of going one way or the other, you are going to have a lot of pipeline stalls.

This is why eliminating if statements are good, especially in tight loops.

This seems to have some good explanation: http://igoro.com/archive/fast-and-slow-if-statements-branch-prediction-in-modern-processors/

like image 55
Salgar Avatar answered Sep 28 '22 04:09

Salgar