c - Should I remove unnecessary `else` in `else if`? -
compare two:
if (strstr(a, "earth")) // a1 return x; if (strstr(a, "ear")) // a2 return y;
and
if (strstr(a, "earth")) // b1 return x; else if (strstr(a, "ear")) // b2 return y;
personally, feel else
redundant , prevent cpu branch prediction.
in first one, when executing a1, it's possible pre-decode a2. , in second one, not interpret b2 until b1 evaluated false.
i found lot of (maybe of?) sources using latter form. though, latter form looks better understand, because it's not call return y
if a =~ /ear(?!th)/
without else clause.
your compiler knows both these examples mean same thing. cpu branch prediction doesn't come it.
i choose first option symmetry.
Comments
Post a Comment