# Mysterious runtime difference

**URL:** <https://discourse.julialang.org/t/mysterious-runtime-difference/95234>\
**Category:** Performance\
**Created:** [February 26, 2023, 8:33pm UTC](https://discourse.julialang.org/t/mysterious-runtime-difference/95234 "2023-02-26T20:33:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [February 26, 2023, 8:33pm UTC](https://discourse.julialang.org/t/mysterious-runtime-difference/95234/1 "2023-02-26T20:33:45Z")

</div>

I would expect the two functions

```julia
f1(a; dims=:) = reduce(+, a; dims)
f2(a, b; dims=:) = b ? reduce(+, a; dims) : reduce(+, a; dims)

```

to have virtually identical runtime, but that is not the case:

```julia
julia> a=[1,2,3];
julia> @btime f1($a)
  5.615 ns (0 allocations: 0 bytes)
julia> @btime f2($a, true)
  328.360 ns (0 allocations: 0 bytes)

```

Tried in Julia 1.8.2, 1.9.0-beta4 and master. Is this a bug?

EDIT: One can make the difference even worse:

```julia
g1(a; dims=:) = invoke(reduce, Tuple{Any,AbstractArray}, +, a; dims)

function g2(a, b; dims=:)
    if b
        invoke(reduce, Tuple{Any,AbstractArray}, +, a; dims)
    else
        invoke(reduce, Tuple{Any,AbstractArray}, +, a; dims)    
    end
end

```

```julia
julia> @btime g1($a)
  5.298 ns (0 allocations: 0 bytes)
julia> @btime g2($a, true)
  1.520 μs (8 allocations: 496 bytes)

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 26, 2023, 9:23pm UTC](https://discourse.julialang.org/t/mysterious-runtime-difference/95234/2 "2023-02-26T21:23:06Z")

</div>

Appears to be related to `f2` not specializing on its own default keyword argument (as a result of `:` subtyping `Function`, as mentioned in [performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing)):

```julia
julia> f1(a; dims=:) = reduce(+, a; dims)
       f2(a, b; dims=:) = b ? reduce(+, a; dims) : reduce(+, a; dims)
       f3(a, b; dims::T=:) where T = b ? reduce(+, a; dims) : reduce(+, a; dims)
f3 (generic function with 1 method)

julia> a = [1,2,3]
       @btime f1($a)
       @btime f2($a, true)
       @btime f3($a, true)
  4.200 ns (0 allocations: 0 bytes)
  312.000 ns (0 allocations: 0 bytes)
  6.100 ns (0 allocations: 0 bytes)
6

```

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [February 26, 2023, 9:43pm UTC](https://discourse.julialang.org/t/mysterious-runtime-difference/95234/3 "2023-02-26T21:43:25Z")

</div>

Great, thanks!

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 26, 2023, 9:44pm UTC](https://discourse.julialang.org/t/mysterious-runtime-difference/95234/4 "2023-02-26T21:44:46Z")

</div>

Is there good reason for `:` to subtype `Function`, I wonder? 🤔
