# Performance of functions with keyword arguments

**URL:** https://discourse.julialang.org/t/performance-of-functions-with-keyword-arguments/5916
**Category:** General Usage
**Created:** [September 16, 2017, 7:53am UTC](https://discourse.julialang.org/t/performance-of-functions-with-keyword-arguments/5916 "2017-09-16T07:53:01Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [September 16, 2017, 7:53am UTC](https://discourse.julialang.org/t/performance-of-functions-with-keyword-arguments/5916/1 "2017-09-16T07:53:01Z")

</div>

In the manual, we [read](https://docs.julialang.org/en/latest/manual/performance-tips/):

> Functions with keyword arguments have near-zero overhead for call sites that pass only positional arguments.

Does this mean that functions with keyword arguments are as fast as functions without keyword arguments as long as they don’t use any keyword arguments? Or that keyword arguments don’t affect performance if there are no optional arguments?

An example might help me understand this… 🙂

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [September 16, 2017, 9:22am UTC](https://discourse.julialang.org/t/performance-of-functions-with-keyword-arguments/5916/2 "2017-09-16T09:22:30Z")

</div>

might want to edit the title 😃

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 16, 2017, 9:54am UTC](https://discourse.julialang.org/t/performance-of-functions-with-keyword-arguments/5916/3 "2017-09-16T09:54:53Z")

</div>

Perhaps:

```julia
julia> f_kw(x; y = 3) = x + y
f_kw (generic function with 1 method)

julia> f(x, y = 3) = x + y
f (generic function with 2 methods)

julia> @btime f_kw(2);
  1.789 ns (0 allocations: 0 bytes)

julia> @btime f_kw(2, y = 3);
  66.668 ns (1 allocation: 96 bytes)

julia> @btime f(2);
  1.789 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [September 16, 2017, 3:51pm UTC](https://discourse.julialang.org/t/performance-of-functions-with-keyword-arguments/5916/4 "2017-09-16T15:51:17Z")

</div>

Thanks! So, functions with keyword arguments are slower if you actually use the keyword arguments… This seems a bit disappointing (?); perhaps I’ve over-used them in the past, and should think about other techniques for specifying options…

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [September 16, 2017, 5:50pm UTC](https://discourse.julialang.org/t/performance-of-functions-with-keyword-arguments/5916/5 "2017-09-16T17:50:36Z")

</div>

Yes. Essentially they cannot be inferred in the current setup, so keyword arguments actually have a dynamic dispatching function barrier in there which adds the cost. You can see this in action if you `@code_typed` and see that you don’t get back your real function and instead get back a function which calls an internal function. This will be changed when `NamedTuple`s are in Base because then it will be able to infer.

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [September 16, 2017, 5:53pm UTC](https://discourse.julialang.org/t/performance-of-functions-with-keyword-arguments/5916/6 "2017-09-16T17:53:12Z")

</div>

Cool, thanks. So if I wait (till 0.7)? I don’t have to work out an alternative to keyword arguments. Sounds like a plan.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [September 16, 2017, 5:54pm UTC](https://discourse.julialang.org/t/performance-of-functions-with-keyword-arguments/5916/7 "2017-09-16T17:54:37Z")

</div>

Yeah, it seems like it’s v0.7 which will have it. I stopped worrying about this awhile ago: Julia moves fast enough that you can just program for the future Julia instead of spending time with current limitations.
