I found the nested if
run faster than serialized if
and it confused me.
I have a code that needs to be run 100 million times:
if(b8 < 254) {
if(b8 < 252)
return data[b8];
return random_bit(&cnt, &b64, rng) ? data[b8] : -data[b8];
}
The b8
is an uint8_t
random number and random_bit(&cnt, &b64, rng)
return a 0-1 random number. This fragment will return some value with 2 if-judgments and I realized I could rewrite it as
if(b8 < 252)
return data[b8];
if(b8 < 254)
return random_bit(&cnt, &b64, rng) ? data[b8] : -data[b8];
Now, return data[b8]
only needs 1 if-judgements. However, I noticed that the program slowed down by about 0.01s with 100 million repetitions. For some reason I need this code to run faster, even if it’s 0.01s. So did I misunderstand anything about this code?
BTW, the code is compiled with -Ofast
and the assembly code is too complex to understand for me.