# Fastest approach to returning a vector of elements choose 2

**URL:** <https://discourse.julialang.org/t/fastest-approach-to-returning-a-vector-of-elements-choose-2/110494>\
**Category:** Performance\
**Tags:** question\
**Created:** [February 21, 2024, 3:31am UTC](https://discourse.julialang.org/t/fastest-approach-to-returning-a-vector-of-elements-choose-2/110494 "2024-02-21T03:31:07Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![narnold0](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/narnold0/32/206975_2.png) [@narnold0](https://discourse.julialang.org/u/narnold0)\
**Post date:** [February 22, 2024, 6:07am UTC](https://discourse.julialang.org/t/fastest-approach-to-returning-a-vector-of-elements-choose-2/110494/21 "2024-02-22T06:07:16Z")

</div>

> [@PeterSimon](#):
>
> sooner or later you have to pay for the calculations you are doing.

Makes sense. I tried threading what I posted before with the choose2 generator that @abraemer posted, but the threading doesn’t like the generator:

```julia
TaskFailedException

    nested task error: ArgumentError: Iterates of the argument to Flatten are not known to have constant length
    Stacktrace:
     [1] flatten_length(f::Base.Iterators.Flatten{Base.Generator{UnitRange{Int64}, var"#993#994"{Vector{Line{2, Float32}}}}}, T::Type)
       @ Base.Iterators .\iterators.jl:1185
     [2] length(f::Base.Iterators.Flatten{Base.Generator{UnitRange{Int64}, var"#993#994"{Vector{Line{2, Float32}}}}})
       @ Base.Iterators .\iterators.jl:1187
     [3] (::var"#402#threadsfor_fun#997"{var"#402#threadsfor_fun#996#998"{Vector{Tuple{Line{2, Float32}, Line{2, Float32}, Point{2, Float32}}}, Base.Iterators.Flatten{Base.Generator{UnitRange{Int64}, var"#993#994"{Vector{Line{2, Float32}}}}}}})(tid::Int64; onethread::Bool)
       @ Main .\threadingconstructs.jl:132
     [4] (::var"#402#threadsfor_fun#997"{var"#402#threadsfor_fun#996#998"{Vector{Tuple{Line{2, Float32}, Line{2, Float32}, Point{2, Float32}}}, Base.Iterators.Flatten{Base.Generator{UnitRange{Int64}, var"#993#994"{Vector{Line{2, Float32}}}}}}})(tid::Int64)
       @ Main .\threadingconstructs.jl:130
     [5] (::Base.Threads.var"#1#2"{var"#402#threadsfor_fun#997"{var"#402#threadsfor_fun#996#998"{Vector{Tuple{Line{2, Float32}, Line{2, Float32}, Point{2, Float32}}}, Base.Iterators.Flatten{Base.Generator{UnitRange{Int64}, var"#993#994"{Vector{Line{2, Float32}}}}}}}, Int64})()
       @ Base.Threads .\threadingconstructs.jl:108

```

Is it possible to thread a generator like that without having to just collect it?

```julia
intersections = Vector{Tuple{GeometryBasics.Line{2, Float32}, GeometryBasics.Line{2, Float32}, GeometryBasics.Point{2,Float32}}}() 
  
Threads.@threads for (seg1, seg2) in choose2_generator(segments)  
    x1, y1, x2, y2, x3, y3, x4, y4, t, u = line_data1(seg1, seg2,)
    
        if is_intersecting(t, u)
            point = lineintersection(x1, y1, x1-x2, y1-y2, x3, y3, x3-x4, y3-y4, t, u)
            if point != false && not_on_endpoint(seg1, seg2, point)
    
                push!(intersections, (seg1, seg2, point) )
            end
   
        end
            
    end

```

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [February 22, 2024, 6:16am UTC](https://discourse.julialang.org/t/fastest-approach-to-returning-a-vector-of-elements-choose-2/110494/22 "2024-02-22T06:16:42Z")

</div>

I don’t think you can parallelize construction of a generator. For your situation, where you don’t just want the n-choose-2 pairs, but you need to do significant calculations to determine which to keep, you probably don’t want a generator. You should write functions to do your selection as rapidly and with as few allocations as possible, and then just include them in a loop that you multithread. That’s my take, perhaps others here have better ideas.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [February 22, 2024, 6:18am UTC](https://discourse.julialang.org/t/fastest-approach-to-returning-a-vector-of-elements-choose-2/110494/23 "2024-02-22T06:18:46Z")

</div>

> [@narnold0](#):
>
> the threading doesn’t like the generator:

Because the generator requires that you iterate it sequentially, and `@threads` doesn’t do that.

[Previous page](https://discourse.julialang.org/t/fastest-approach-to-returning-a-vector-of-elements-choose-2/110494.md?page=1)
