# What is Julia's maxk(Matlab) that returns the indice of top K largest values?

**URL:** <https://discourse.julialang.org/t/what-is-julias-maxk-matlab-that-returns-the-indice-of-top-k-largest-values/14100>\
**Category:** General Usage\
**Tags:** sort, sortperm\
**Created:** [August 26, 2018, 10:48pm UTC](https://discourse.julialang.org/t/what-is-julias-maxk-matlab-that-returns-the-indice-of-top-k-largest-values/14100 "2018-08-26T22:48:09Z")\
**Posts on this page:** 1\
**Page:** 2

<div class="post-metadata">

**Author:** ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)\
**Post date:** [May 9, 2023, 4:19pm UTC](https://discourse.julialang.org/t/what-is-julias-maxk-matlab-that-returns-the-indice-of-top-k-largest-values/14100/22 "2023-05-09T16:19:51Z")

</div>

Normally you would just do

```julia
static_topk(x, k) = _topk_static_dispatch(x, Val(k))

```

rather than `PREALLOC_VALS[k]`.

But in another project I found you can get a tiny performance boost if you create all the `Val(i)` beforehand and just index it. I didn’t test whether it affects this one though; it’s just a habit (perhaps a bad one).

The `::Val{k}` is just to make the function get recompiled each time k changes, so that the static arrays are type stable. (`@generated` only re-generates the functions when types change, so you couldn’t get around using the `Val{k}` here).

(I remember reading that in the most recent Julia versions, the compiler tends to be good enough so you don’t need `@generated` in most scenarios, so you can just let type specialization handle everything.)

[Previous page](https://discourse.julialang.org/t/what-is-julias-maxk-matlab-that-returns-the-indice-of-top-k-largest-values/14100.md?page=1)
