I try to make very time efficient code in C++. I was told that I should avoid if conditions whenever possible. So I was thinking that type conversion could do the job. The code with if condition would look like this:
double a = .512; // some real number
double x = 1.1; // a coordinate that gets changed when if condition is true
a *= a; // a squared
if(a >= 1){x += .1;}
I would avoid the if condition in the following way.
double a = .512; // some real number
double x = 1.1; // a coordinate that gets changed when if condition is true
a *= a; // a squared
x += static_cast<bool>(static_cast<int>(a)) * .1
This first converts a
into an int
. This gives 0
for a<1
and a non-zero int
for a>1
. The second conversion then converts all non-zero int
s to true
. But is it really faster? Are there any problems I could run into using this method?
Aucun commentaire:
Enregistrer un commentaire