# Why is ternary operator two times faster than if statement?

**URL:** <https://discourse.julialang.org/t/why-is-ternary-operator-two-times-faster-than-if-statement/644>\
**Category:** New to Julia\
**Created:** [November 30, 2016, 12:53am UTC](https://discourse.julialang.org/t/why-is-ternary-operator-two-times-faster-than-if-statement/644 "2016-11-30T00:53:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vejitatoja](https://avatars.discourse-cdn.com/v4/letter/v/8edcca/32.png) [@vejitatoja](https://discourse.julialang.org/u/vejitatoja)\
**Post date:** [November 30, 2016, 12:53am UTC](https://discourse.julialang.org/t/why-is-ternary-operator-two-times-faster-than-if-statement/644/1 "2016-11-30T00:53:35Z")

</div>

```julia
function with_ternary()
  sum(x%3==0 || x%5==0 ? x : 0 for x=1:999999999)
end

function with_if()
  sum(x for x=1:999999999 if x%3 == 0 || x%5 == 0)
end

```

I call @time with\_if() and @time with\_ternary() several times after that to make sure it’s already compiled. The results show me that with\_ternary() funcition is 2 times faster than with\_if() function. Can someone explain to why is that the case?

I know that two functions are not exactly the same because with\_ternary() is passing zeros to the sum() function. So why is it so much faster?

```julia
@time with_ternary()
233333333166666668
2.206624 seconds (7 allocations: 240 bytes)

```

```julia
@time with_if()
233333333166666668
4.835853 seconds (12 allocations: 416 bytes)

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 30, 2016, 1:15am UTC](https://discourse.julialang.org/t/why-is-ternary-operator-two-times-faster-than-if-statement/644/2 "2016-11-30T01:15:39Z")

</div>

The two functions are not equivalent the equivalent to the ternary operator version is

```julia
julia> function with_if2()
         sum(if x%3 == 0 || x%5 == 0 x else 0 end for x=1:999999999)
       end

```

Which should give you similar performance.

---

<div class="post-metadata">

**Author:** ![akis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akis/32/156_2.png) [@akis](https://discourse.julialang.org/u/akis)\
**Post date:** [November 30, 2016, 1:21am UTC](https://discourse.julialang.org/t/why-is-ternary-operator-two-times-faster-than-if-statement/644/3 "2016-11-30T01:21:23Z")

</div>

Does this mean that conditional [generator expressions](http://docs.julialang.org/en/release-0.5/manual/arrays/#generator-expressions) are slow?

---

<div class="post-metadata">

**Author:** ![vejitatoja](https://avatars.discourse-cdn.com/v4/letter/v/8edcca/32.png) [@vejitatoja](https://discourse.julialang.org/u/vejitatoja)\
**Post date:** [November 30, 2016, 10:13am UTC](https://discourse.julialang.org/t/why-is-ternary-operator-two-times-faster-than-if-statement/644/4 "2016-11-30T10:13:33Z")

</div>

Thank you yuyichao. Function with\_if2() that you proposed gives same performance as with\_ternary().

But why expression without “else” is 2 times slower

```julia
 sum(x for x=1:999999999 if x%3 == 0 || x%5 == 0)

```

in comparison to the ternary syntax

```julia
sum(if x%3 == 0 || x%5 == 0 x else 0 end for x=1:999999999)

```

I want to understand how things work underneath to write functions in an optimal way

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 30, 2016, 5:20pm UTC](https://discourse.julialang.org/t/why-is-ternary-operator-two-times-faster-than-if-statement/644/5 "2016-11-30T17:20:53Z")

</div>

Going though a filter iterator will be a little slower.
