More efficient way of getting permutations

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.

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 :slight_smile:

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> @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"
1 Like

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?

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

3 Likes