Warm tip: This article is reproduced from stackoverflow.com, please click
optimization julia

Julia equivalent to the python consecutive_groups() function

发布于 2020-04-03 23:21:25

I am looking for a julia alternative with the same behavior as more_itertools.consecutive_groups in python.

I came up with a simple implementation but speed is an issue here and I'm not sure if the code is optimized enough.

function consecutive_groups(array)
    groups = eltype(array)[]
    j = 0
    for i=1:length(array)-1
        if array[i]+1 != array[i+1]
            push!(groups, array[j+1:i])
            j = i
        end
    end
    push!(groups, array[j+1:end])
    return groups
end
Questioner
Thomas Jalabert
Viewed
49
laborg 2020-01-31 21:41

Your implementation is already quite fast. If you know that the consecutive groups will be large you might want to just increase the index instead of pushing every element:

function consecutive_groups_2(v)
  n = length(v)
  groups = Vector{Vector{eltype(v)}}()

  i = j = 1
  while i <= n && j <= n
    j = i
    while j < n  && v[j] + 1 == v[j + 1] 
      j += 1
    end
    push!(groups,v[i:j])
    i = j + 1
  end

  return groups
end

which is roughly 33% faster on large groups:

julia> x = collect(1:100000);

julia> @btime consecutive_groups(x)
  165.939 μs (4 allocations: 781.45 KiB)
1-element Array{Array{Int64,1},1}:
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10  …  99991, 99992, 99993, 99994, 99995, 99996, 99997, 99998, 99999, 100000]

julia> @btime consecutive_groups_2(x)
  114.830 μs (4 allocations: 781.45 KiB)
1-element Array{Array{Int64,1},1}:
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10  …  99991, 99992, 99993, 99994, 99995, 99996, 99997, 99998, 99999, 100000]