jeudi 26 janvier 2017

Is there "compiler-friendly" code / convention

To specify my Question a bit. I know Code should be readable by Humans in the very first place. But I want to know if there are certain tricks which can add up to the performance a bit.

  1. If there is an Event, that i know will be true 80% of the time. Where should i put the Code in the If clause in general?

    if(Event){ 
        //put here the likely to happen? 
    }else{
        //or here?
    }
    
    

My thought on this: Most modern Processors guess which branch they take and analyse their decisions. But in this case i know which guess would be better. Can i support this in any way?

  1. Should i use Expressions like this: x << 2 whenever i can, or should the compiler do this work for the sake of readability?

  2. Should i generally speaking, use recursive functions, which i find more readable. Or iterative functions which can be more stable. e.g Fibonacci results in a Stack-overflow on relative small inputs when done recursive.
    Whereas an iterative implementation is much more robust.

To sum up: Are there small things i can do to support the real optimisation done by the compiler (gcc in my case)?
Are those examples to consider or should i not think about this at all?

I hope this Question is not too theoretical. Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire