Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Broadcasting a splat #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Broadcasting a splat #44

wants to merge 3 commits into from

Conversation

mcabbott
Copy link
Owner

@mcabbott mcabbott commented Apr 4, 2021

On master the following syntax is understood:

julia> using TensorCast, ImageCore

julia> mat = rand(1:10, 4, 3) ./ 10;

julia> @cast _[k] := RGB(mat[k,:]...)
4-element Array{RGB{Float64},1} with eltype RGB{Float64}:
 RGB{Float64}(0.8,0.7,0.7)
 RGB{Float64}(0.5,0.7,0.5)
 RGB{Float64}(0.9,0.7,0.2)
 RGB{Float64}(0.7,0.2,1.0)

But this is done quite inefficiently, by literally performing the splat for every slice. It would be better to re-write it to slice the other way, splat once, and then broadcast:

julia> @btime Base.splat(RGB).(eachrow(m))  setup=(m=rand(100,3));
 22.541 μs (1122 allocations: 38.89 KiB)

julia> @btime RGB.(eachcol(m)...)  setup=(m=rand(100,3));
 421.905 ns (10 allocations: 3.12 KiB)

Can this be done in general?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant