# Faster shuffle! for small arrays?

**URL:** <https://discourse.julialang.org/t/faster-shuffle-for-small-arrays/2100>\
**Category:** General Usage\
**Created:** [February 14, 2017, 2:57pm UTC](https://discourse.julialang.org/t/faster-shuffle-for-small-arrays/2100 "2017-02-14T14:57:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)\
**Post date:** [February 14, 2017, 2:57pm UTC](https://discourse.julialang.org/t/faster-shuffle-for-small-arrays/2100/1 "2017-02-14T14:57:52Z")

</div>

I have some simulation code that I’m trying to speed up. `@code_warntype` didn’t suggest any type instability, so I turned to profiling. The core of the simulation code is essentially:

```julia
function core_of_simulation()
    choices = get_choices()
    shuffle!(choices)
    for c in choices
        success = attempt_choice!(c)
        if success
            return true
        end
    end
    return false
end

```

(It’s a customer choice model simulation which computes which discrete choices are optimal for the customer, then the customer chooses randomly from these good choices. I shuffle and then go one-by-one rather than just randomly selecting since some of the options may not be feasible choices for the customer.)

About half of simulation time is spent in the call to `shuffle!`. Almost always the list of choices being shuffled has length \<10, and usually \<5. Maybe there’s no efficiency gain to be had here, but it seems a little surprising that the simulation would have to spend more than half its time shuffling very short lists. Knowing that a list is short, is there a more efficient technique? I played around a bit but unsurprisingly was unable to beat the built-in `shuffle!`.

There is also the option of not shuffling at all but rather first filtering the list of choices to only feasible choices, and then selecting one at random. Checking choice feasibility is a bit expensive so if the shuffling can be sped up that seems preferable.

Many thanks for the help.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 14, 2017, 3:31pm UTC](https://discourse.julialang.org/t/faster-shuffle-for-small-arrays/2100/2 "2017-02-14T15:31:37Z")

</div>

You could try something like this (untested code).

```julia
function core_of_simulation()
    choices = get_choices()
    n = length(choices)
    while n > 0
        i = rand(1:n)
        if attempt_choice!(choices[i])
            return true
        end
        choices[i], choices[n] = choices[n], choices[i]
        n -= 1
    end
    return false
end

```

It can be restructured a bit for more efficiency when `n==1` but you get the idea.

---

<div class="post-metadata">

**Author:** ![pint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pint/32/125_2.png) [@pint](https://discourse.julialang.org/u/pint)\
**Post date:** [February 14, 2017, 3:59pm UTC](https://discourse.julialang.org/t/faster-shuffle-for-small-arrays/2100/3 "2017-02-14T15:59:39Z")

</div>

you could try `randperm`, and access items randomly instead of shuffling. but if your bottleneck is the rng, it won’t help. hard to tell without actual times and types.
