# Boost the performance of partition 1:n into equivalence classes

**URL:** https://discourse.julialang.org/t/boost-the-performance-of-partition-1-n-into-equivalence-classes/64564
**Category:** New to Julia
**Tags:** performance
**Created:** [July 13, 2021, 12:55pm UTC](https://discourse.julialang.org/t/boost-the-performance-of-partition-1-n-into-equivalence-classes/64564 "2021-07-13T12:55:13Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://discourse.julialang.org/u/Thomas)
#### Post date: [July 13, 2021, 12:55pm UTC](https://discourse.julialang.org/t/boost-the-performance-of-partition-1-n-into-equivalence-classes/64564/1 "2021-07-13T12:55:13Z")

</div>

I am trying to speed up the function that make partition of 1:n into equivalence classes according to `boolfn`. I need the partition to be sorted, so using SimplePartition.jl or DataStructures.jl may not be optimized (since disjoint set structure are not sorted, and later sorting caused a lot of overhead).

```julia
function computePartition(n::Int, boolfn)
    indices = collect(1:n)
    part = Vector{Int}[] # can this be optimized, knowing that length(part) <= n?
    it = 0
    while !isempty(indices)
        it += 1
        class = [indices[1]]
        append!(class, [j for j in indices[2:end] if boolfn(indices[1], j)]) # can this be speed up?
        push!(part, class)
        filter!(k->!(k in class), indices)
    end
    return part
end

```

My algorithm is not parallelizable at the level of the while loop. However,

```julia
[j for j in indices[2:end] if boolfn(indices[1], j)]

```

may be speed up because the computation can be parallelized. Does Julia parallel the array comprehension?
