# Parity of sorting permutations for vectors with very small size

**URL:** <https://discourse.julialang.org/t/parity-of-sorting-permutations-for-vectors-with-very-small-size/95172>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 24, 2023, 11:39pm UTC](https://discourse.julialang.org/t/parity-of-sorting-permutations-for-vectors-with-very-small-size/95172 "2023-02-24T23:39:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)\
**Post date:** [February 24, 2023, 11:39pm UTC](https://discourse.julialang.org/t/parity-of-sorting-permutations-for-vectors-with-very-small-size/95172/1 "2023-02-24T23:39:52Z")

</div>

What would be the fastest way to get the parity of sorting permutations for vectors with very small size?

The obvious way is to use the `sortperm!` function and the [parity](https://juliamath.github.io/Combinatorics.jl/dev/api/#Combinatorics.parity-Tuple%7BAbstractVector%7Bvar%22#s38%22%7D%20where%20var%22#s38%22%3C:Integer%7D) function from `Combinatorics.jl`.

However, `sortperm!` costs 85ns on my laptop:

```julia
julia> p = zeros(Int64, 8);

julia> v = rand(Int8, 8);

julia> @btime sortperm!($p, $v);
  85.254 ns (1 allocation: 16 bytes)

julia> using Combinatorics

julia> parity(p)
1

```

I need to calculate the parity for billions of times. Given that the vectors have very small size: (\<15), is there any optimization I can do to improve the performance?
