# Evaluate Piecewise Functions Effciently

**URL:** <https://discourse.julialang.org/t/evaluate-piecewise-functions-effciently/55832>\
**Category:** Performance\
**Tags:** question\
**Created:** [February 23, 2021, 9:30am UTC](https://discourse.julialang.org/t/evaluate-piecewise-functions-effciently/55832 "2021-02-23T09:30:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![drarnau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drarnau/32/4476_2.png) [@drarnau](https://discourse.julialang.org/u/drarnau)\
**Post date:** [February 23, 2021, 9:30am UTC](https://discourse.julialang.org/t/evaluate-piecewise-functions-effciently/55832/1 "2021-02-23T09:30:32Z")

</div>

Dear all,

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:

```julia
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!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 23, 2021, 10:12am UTC](https://discourse.julialang.org/t/evaluate-piecewise-functions-effciently/55832/2 "2021-02-23T10:12:40Z")

</div>

Check this older [post](https://discourse.julialang.org/t/performant-piecewise-function-evaluation/45762) on same topic.

---

<div class="post-metadata">

**Author:** ![drarnau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drarnau/32/4476_2.png) [@drarnau](https://discourse.julialang.org/u/drarnau)\
**Post date:** [February 23, 2021, 11:40am UTC](https://discourse.julialang.org/t/evaluate-piecewise-functions-effciently/55832/3 "2021-02-23T11:40:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [February 23, 2021, 11:54am UTC](https://discourse.julialang.org/t/evaluate-piecewise-functions-effciently/55832/4 "2021-02-23T11:54:46Z")

</div>

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).

FWIW:

```julia
julia> @btime myPiecewise($(Ref(1))[],$(Ref(2))[]);
  3.456 ns (0 allocations: 0 bytes)

```

Out of curiosity (and stupidity) I tested `ifelse` that avoids the branch. The timing is the same.

```julia
julia> nobranch(a,b) = ifelse(a<0,0.5*a+b,-2.5*a+b)
nobranch (generic function with 1 method)

julia> @code_llvm debuginfo=:none nobranch(1,2)

define double @julia_nobranch_685(i64, i64) {
top:
  %2 = icmp sgt i64 %0, -1
  %3 = sitofp i64 %0 to double
  %4 = sitofp i64 %1 to double
  %.p.v = select i1 %2, double -2.500000e+00, double 5.000000e-01
  %.p = fmul double %.p.v, %3
  %5 = fadd double %.p, %4
  ret double %5
}

julia> @btime nobranch($(Ref(1))[],$(Ref(2))[]);
  3.456 ns (0 allocations: 0 bytes)

```
