# \`sort\` vs \`copy\`+\`sort!\`

**URL:** <https://discourse.julialang.org/t/sort-vs-copy-sort/91919>\
**Category:** Performance\
**Tags:** sort\
**Created:** [December 20, 2022, 10:36pm UTC](https://discourse.julialang.org/t/sort-vs-copy-sort/91919 "2022-12-20T22:36:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 20, 2022, 10:36pm UTC](https://discourse.julialang.org/t/sort-vs-copy-sort/91919/1 "2022-12-20T22:36:03Z")

</div>

This is run on Julia 1.8.3

```julia
using BenchmarkTools

function benchsort(L)
    v = rand(L)
    @show L
    @btime sort($v)
    @btime sort!($v)
    @btime sort!(copy($v))
    nothing
end

```

Running this with 2048 length vectors (same results apply to any length):

```julia
julia> benchsort(2048)
L = 2048
  74.018 μs (1 allocation: 16.12 KiB)
  11.578 μs (0 allocations: 0 bytes)
  13.083 μs (1 allocation: 16.12 KiB)

```

Why is `sort` so much slower than `sort!` ? And should it be?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [December 20, 2022, 10:44pm UTC](https://discourse.julialang.org/t/sort-vs-copy-sort/91919/2 "2022-12-20T22:44:56Z")

</div>

Sorting first checks if the data is already sorted, and return directly if so. In your example you sort the vector `v` in place with your first call to `sort!`, so the remaining calls will not do any sorting. You can use `setup=(...)` for cases like this (see [BenchmarkTools.jl documentation](https://juliaci.github.io/BenchmarkTools.jl/stable/manual/#Setup-and-teardown-phases)):

```julia
julia> L = 2048; x = rand(L);

julia> @btime sort(v) setup=(v = copy(x)) evals=1;
  56.562 μs (1 allocation: 16.12 KiB)

julia> @btime sort!(v) setup=(v = copy(x)) evals=1;
  55.691 μs (0 allocations: 0 bytes)

julia> @btime sort!(copy(v)) setup=(v = copy(x)) evals=1;
  56.428 μs (1 allocation: 16.12 KiB)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 20, 2022, 11:41pm UTC](https://discourse.julialang.org/t/sort-vs-copy-sort/91919/3 "2022-12-20T23:41:46Z")

</div>

Even this doesn’t seem fireproof. Shouldn’t it have `evals=1`, otherwise you may run into the same problem for the second case, for shorter vectors, for example.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [December 20, 2022, 11:47pm UTC](https://discourse.julialang.org/t/sort-vs-copy-sort/91919/4 "2022-12-20T23:47:19Z")

</div>

You are correct, I updated the post above to include that.
