by paulsutter · 13y ago · 0 comments
Sorry, but "n < 2" is a conditional, whether you're using an if statement, the "?" operator, or to reference an array of function pointers (at least from this old-school C/assembler programmer's perspective). Whereas that Erlang code looks elegantly conditional-free. Clearly branching will appear behind the scenes, but the Erlang code has no conditionals.
Whether that's actually faster is debatable though. If you're writing in C, you're probably concerned about performance, and the function pointer invocation is going to be a lot slower than a branch mispredict. A sufficiently complex array dereference will slow it down too, but I wouldn't be surprised if the compiler can turn simple cases like this into an equivalent switch/jumptable.
Thus conditional-free programming (as presented here, in C) is mostly academic. Debug tracing is an interesting application, but readability and performance both suffer.
If a language could internalize conditional-free constructs natively, however, it could be an interesting study.
set A, 0
ifg B, 2
set A, 1
It conditionally evaluates a machine instruction, which is, indeed, branching.Do you think this is true because C code has traditionally relied so heavily on branching?
Put another way, perhaps branch-free programming is has greater speed potential, but the tools we use have been heavily optimized for branch-laden programming. Maybe putting sufficient resources into optimizing code for branch-free programming (and stripping out optimizations for branching) would yield faster execution.
I don't know the answer, just musing. I am curious, though.
So, I'm going to come down on the side of the traditional model being inherently more performant.