Is it expected that an anonymous function is 300x slower than a named function in sort?
For example:
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.
Looks like you are counting compile time. Consider using BenchmarkTools.
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.
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!
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.