# More efficient way of getting permutations

**URL:** <https://discourse.julialang.org/t/more-efficient-way-of-getting-permutations/66356>\
**Category:** New to Julia\
**Created:** [August 13, 2021, 7:51pm UTC](https://discourse.julialang.org/t/more-efficient-way-of-getting-permutations/66356 "2021-08-13T19:51:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daviddoij](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@daviddoij](https://discourse.julialang.org/u/daviddoij)\
**Post date:** [August 13, 2021, 7:51pm UTC](https://discourse.julialang.org/t/more-efficient-way-of-getting-permutations/66356/1 "2021-08-13T19:51:38Z")

</div>

Hi there,

the following piece of code gets the job done but it is not very efficient. Benchmarks throws:  
0.018436 seconds (7.23 k allocations: 416.604 KiB, 99.90% compilation time)

Timewise it’s ok but I’m wondering about the allocations.

```julia
using Combinatorics

function ProblemXXX()
    digits = [0,1,2,3,4,5,6,7,8,9]
    _permutations = nthperm(digits, 1_000_000)
    return join(_permutations)
end

```

I’m pretty sure it can be done in a more efficient way but I’m still a noob with Julia syntax.

Thx in advance 🙂

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 13, 2021, 7:59pm UTC](https://discourse.julialang.org/t/more-efficient-way-of-getting-permutations/66356/2 "2021-08-13T19:59:05Z")

</div>

> [@daviddoij](#):
>
> 99.90% compilation time

Run it a second time. The first time you run a function, Julia compiles it for you. The second (and 3rd, 4th, 5th, etc) times are significantly faster:

```julia
julia> @time ProblemXXX()
  0.013919 seconds (7.52 k allocations: 450.875 KiB, 99.86% compilation time)
"2783915460"

julia> @time ProblemXXX()
  0.000007 seconds (26 allocations: 1.469 KiB)
"2783915460"

```

---

<div class="post-metadata">

**Author:** ![daviddoij](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@daviddoij](https://discourse.julialang.org/u/daviddoij)\
**Post date:** [August 13, 2021, 8:01pm UTC](https://discourse.julialang.org/t/more-efficient-way-of-getting-permutations/66356/3 "2021-08-13T20:01:42Z")

</div>

Aha! Thanks for the tip!  
Is there a way to supress the output of the first time @macro?  
And can it be displayed in microseconds?

---

<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:** [August 13, 2021, 8:07pm UTC](https://discourse.julialang.org/t/more-efficient-way-of-getting-permutations/66356/4 "2021-08-13T20:07:50Z")

</div>

For microbenchmarks like this, you should use the package BenchmarkTools.jl.
