mardi 19 janvier 2016

Do If-Then Statements Affect Speed of a Processor - Preloading

I've heard that If-Then statements can speed up your code:

[ Does having a lot of if statements degrade rendering speed of php? ] It can speed up your code because it can skip steps.

I also heard from one of my friends that it can slow down code from one of my friends. Consider the following pseudocode:

doStuffThatDoesntInvolveConditionals()

i = floor(random*2)
if (i==1) {
    print "Heads"
    doSomething()
}
else {
    print "Tails"
    doSomethingElse()
}

After this code is compiled and executed, the processor preloads all the stuff in doStuffThatDoesntInvolveConditionals(), but it cannot preload anything after the if-then statement because it cannot evaluate the expression in advance. Therefore, the processing speed would slow down.


  1. Does this preloading happen in this way?
  2. If so, is the amount of which it slows down your program insignificant?
  3. Otherwise, how does the preloading work?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire