# Anoymous Functions Slow in sort

**URL:** <https://discourse.julialang.org/t/anoymous-functions-slow-in-sort/13891>\
**Category:** Performance\
**Tags:** sort\
**Created:** [August 22, 2018, 3:39pm UTC](https://discourse.julialang.org/t/anoymous-functions-slow-in-sort/13891 "2018-08-22T15:39:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabrielgellner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabrielgellner/32/75_2.png) [@gabrielgellner](https://discourse.julialang.org/u/gabrielgellner)\
**Post date:** [August 22, 2018, 3:39pm UTC](https://discourse.julialang.org/t/anoymous-functions-slow-in-sort/13891/1 "2018-08-22T15:39:10Z")

</div>

Is it expected that an anonymous function is 300x slower than a named function in sort?

For example:

```julia
test = rand(1000)
my_id(x) = x
sort(test, by = x -> x) # 0.074194 sec
sort(test, by = identity) # 0.000070 sec
sort(test, by = my_id) # 0.000092 sec

```

In some code I am using have had moved to using named functions for this because of the crazy drop in speed. I thought anonymous functions where equivalent to named functions for speed these days. Am I doing something silly?

Thanks so much.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [August 22, 2018, 3:46pm UTC](https://discourse.julialang.org/t/anoymous-functions-slow-in-sort/13891/2 "2018-08-22T15:46:11Z")

</div>

Looks like you are counting compile time. Consider using BenchmarkTools.

```julia
julia> @btime sort($test, by=$(x->x));
  39.904 μs (2 allocations: 7.95 KiB)

julia> @btime sort($test, by=$my_id);
  41.423 μs (2 allocations: 7.95 KiB)

julia> @btime sort($test, by=$identity);
  15.551 μs (2 allocations: 7.95 KiB)

```

Not sure why `identity` is so much faster, that seems unfortunate.

---

<div class="post-metadata">

**Author:** ![gabrielgellner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabrielgellner/32/75_2.png) [@gabrielgellner](https://discourse.julialang.org/u/gabrielgellner)\
**Post date:** [August 22, 2018, 3:56pm UTC](https://discourse.julialang.org/t/anoymous-functions-slow-in-sort/13891/3 "2018-08-22T15:56:53Z")

</div>

Sweet! That makes a tonne of sense. Now I need to find out why my full function is slow as clearly it is not this!

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 22, 2018, 5:05pm UTC](https://discourse.julialang.org/t/anoymous-functions-slow-in-sort/13891/4 "2018-08-22T17:05:07Z")

</div>

> [@ExpandingMan](#):
>
> Not sure why `identity` is so much faster, that seems unfortunate.

Identity sorting chooses the algorithm based on element type, and can use quick sort for numbers. I think that non-identity sorting uses merge sort instead to guarantee stability. You can choose a different sorting algorithm explicitly though.
