Skip to content

Instantly share code, notes, and snippets.

@aviatesk
Last active September 14, 2020 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aviatesk/c1026108f94a956db8dd3d9758739243 to your computer and use it in GitHub Desktop.
Save aviatesk/c1026108f94a956db8dd3d9758739243 to your computer and use it in GitHub Desktop.
flatten flatten
using BenchmarkTools, Cthulhu
f(a, b) = collect(Iterators.flatten((a, b)))
a = rand(10^6)
b = rand(10^6)
@btime f($a, $b) # 14.402 ms (21 allocations: 17.00 MiB)
# @descend f(a, b)
(@code_typed iterate(Iterators.flatten((a,b)))) |> last # ::Union{Nothing, Tuple{Float64, Tuple{Int64, Vector{Float64}, Int64}}} -> unwidened
a′ = rand(10^6)
b′ = @view a′[end-1:-1:2]
@btime f($a′, $b′) # 242.919 ms (8000021 allocations: 405.73 MiB)
# @descend f(a′, b′)
(@code_typed iterate(Iterators.flatten((a′,b′)))) |> last # ::Union{Nothing, Tuple{Float64, Tuple{Union{Int64, SubArray{Float64, 1, Vector{Float64}, Tuple{StepRange{Int64, Int64}}, true}, Vector{Float64}}, Union{Int64, SubArray{Float64, 1, Vector{Float64}, Tuple{StepRange{Int64, Int64}}, true}, Vector{Float64}}, Union{Int64, Tuple{Base.OneTo{Int64}, Int64}}}}} -> too complex, widened
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment