I would say it depends on how the compiler will optimize your code.
Well, if you want to get that pedantic about it (and apparently you do
), it also depends on how the CPU will optimize the code.
To me the first version looks more efficient.
Unless you're a compiler, I'm not sure that's a useful metric.
You don't want any conditional
branch statements in the assembler code if you can help it, branching is
expensive in assembler (resets the pipeline).
Branching in and of itself does not "reset the pipeline". Between having
multiple pipelines and branch prediction, simple branching by itself is
not necessarily a problem at all.
Beyond that, there's not really any difference with respect to branching
between the two versions. The compiler can optimize either into an
assumed "straight-through" case if it wants to. Though, the last time I
looked at this sort of optimization (granted, years ago), the "assumed"
case was the "true" if() statement, which means that in the former, the
optimized version will optimize for the failure case. Probably not the
best thing to do.
Besides, as far as "conditional branch statements" go, both versions have
the same nominal number of conditionals. The main difference is that the
latter version takes advantage of information generated by them to avoid
later computations. Avoiding unnecessary computations is a fundamental
technique in optimization.
(There is, of course, some chance that the compiler would notice the
opportunity to remove the unnecessary computations. But that would depend
on it having specific knowledge of the char to int conversion and relating
that to the constraint of the possible values of c by the time it gets to
the computation. I would be pretty surprised if it did pick up on that,
even as good as optimizing compilers are these days).
Of course, if the performance of this method is so critical it's worth
anywhere close to the scrutiny it's receiving here, the real issue is to
figure out why an application spends so much time converting strings to
their binary equivalent.
Pete