5 ms·
I had a professor in university who was frequently saying he developed a thousand application and he didn't use any if. I know it's extreme but is there any wa
by yildirim 10y ago
I had a professor in university who was frequently saying he developed a thousand application and he didn't use any if.
I know it's extreme but is there any way to reduce (lets say 5 to 1) conditions?
I think he was developing Fortran apps.
Anecdote: This very same professor asked us to do a matrix operation (I don't remember what) without using if once. He said it would be faster than using ifs. Many of us couldn't. Then he revealed his solution, he was getting first indice of first row and doing some operation and getting the last indice of last row, then second indice of first row and so on.
One of my friend told that if he wrote the algorithm plain using ifs, it would be faster. Professor told it's impossible.
In the end of the day the version which was written using ifs was a lot faster than the version without ifs. Because of the changing indices cause a lot of cache misses but getting matrix elements sequentially used cache correctly.
- hvidgaard 10y agoThe performance cost of ifs are not that bad. Especially if it means you can read sequentially from the memory. It however, do represent branching, and in a hotspot it can be more efficient to simply calculate both the if and the else and just choose the right value, rather than having a branch misprediction. In any case, always profile, and identify issues before doing any optimization.
- bluetomcat 10y agoPrograms with lots of deep branches are generally not well thought out and prone to bugs. In my view, every branch represents an "exception" to the main execution path. The explosion of combinations of taken/not taken branches is what brings your program to an unpredictable state. The performance side is debatable, but with fewer branches a program is definitely simpler and more elegant.