I have a very basic (maybe stupid) question. I need to evaluate a piecewise function many times. My naive approach is to do something like:
function myPiecewise(a,b)
if a < 0
return 0.5*a + b
else
return - 2.5*a + b
end
end
Is that an efficient way of implementing piecewise functions? I could just create two functions, one for the negative case and another for the positive case. Would that be better in terms of performance?
Thanks. I saw that post before posting my question. As far as I understand, in their case, they can’t increase performance using the alternative to the naive solution. That’s why I posted my question.
I would think that your implementation is fast. After all, it is only a single branch and should only take a few nanoseconds to run. Unless this is a serious performance bottleneck (which I doubt) I wouldn’t care about optimising it (if possible).