Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

document the performance caveat on abstract eltype #145

Open
johnnychen94 opened this issue Jul 1, 2020 · 1 comment
Open

document the performance caveat on abstract eltype #145

johnnychen94 opened this issue Jul 1, 2020 · 1 comment
Labels
best practice workflows and best practice that are useful in JuliaImages and/or Julia

Comments

@johnnychen94
Copy link
Member

johnnychen94 commented Jul 1, 2020

Gray.(img) is used widely tha Array{Gray, 2}(undef, sz...) or zeros(Gray, sz) becomes very alluring. However, there's a significant performance gap here:

julia> @btime zeros(Gray, 100, 100);
  39.331 μs (10002 allocations: 234.45 KiB)

julia> @btime zeros(Gray{N0f8}, 100, 100);
  2.565 μs (1 allocation: 9.94 KiB)

julia> @btime Array{Gray, 2}(undef, 100, 100);
  1.841 μs (2 allocations: 78.20 KiB)

julia> @btime Array{Gray{N0f8}, 2}(undef, 100, 100);
  127.157 ns (1 allocation: 9.94 KiB)

This is probably a JuliaImages specific confusion, because when we write Array{AbstractFloat, 2} we know it's an abstract type. But for Gray it's very likely that users don't get this.

Ref: https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-abstract-container-1

@timholy
Copy link
Member

timholy commented Jul 1, 2020

The same issue comes up if you declare a field

struct MyStruct
    A::Array{Float32}
end

because you've failed to specify the dimensionality. It happens for any parametric structure that you don't provide values for all fields.

But I agree that we might want to have a section somewhere early for Julia newbies. As you know, getting this wrong affects not just the initial allocation but also the subsequent usage of the object.

@johnnychen94 johnnychen94 added the best practice workflows and best practice that are useful in JuliaImages and/or Julia label Aug 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
best practice workflows and best practice that are useful in JuliaImages and/or Julia
Projects
None yet
Development

No branches or pull requests

2 participants