In my code, I define several structs in which I carry along an AbstractMatrix
: sometimes it is an actual matrix, sometimes a transpose, etc.
The type of the entries in the matrix also varies (Float64, Int, Dual, etc.).
I was wondering what the idiomatic Julia way of defining containers for such objects is. Currently Iβm using something that resembles ContainerMoreConcrete
, but a quick check shows that ContainerConcrete
has fewer allocations and better speed in my toy example.
Is that the idiomatic way of doing things? Or is a different approach desirable/better? As a possible alternative, I could force every matrix that I use to be a Matrix
, but that seems unnecessarily hurtful for performance? Or is there some hidden benefit to that that Iβm overlooking?
using BenchmarkTools
struct ContainerAbs
x::AbstractMatrix{Real}
end
struct ContainerMoreConcrete{T<:Real}
x::AbstractMatrix{T}
end
struct ContainerConcrete{M<:AbstractMatrix{<:Real}}
x::M
end
function RunCheck(N, a::Real)
display("Benchmarking with datatype $(typeof(a))")
X = transpose([convert(typeof(a), i*j) for i in 1:N, j in 1:N])
ContainerAbs(X); ContainerMoreConcrete(X); ContainerConcrete(X); # Ensure everything is compiled
display(@benchmark ContainerAbs($X))
display(@benchmark ContainerMoreConcrete($X))
display(@benchmark ContainerConcrete($X))
end
RunCheck(10, 1)
RunCheck(10, 1.)
RunCheck(10, 1//1)
gives
"Benchmarking with datatype Int64"
BenchmarkTools.Trial: 10000 samples with 464 evaluations.
Range (min β¦ max): 229.705 ns β¦ 7.052 ΞΌs β GC (min β¦ max): 0.00% β¦ 96.05%
Time (median): 239.043 ns β GC (median): 0.00%
Time (mean Β± Ο): 263.967 ns Β± 146.187 ns β GC (mean Β± Ο): 6.61% Β± 11.23%
βββ
ββββββ β
βββββββββββββββ
β
β
βββββββββββββββββββββββββββββββββββββ
βββββββ β
230 ns Histogram: log(frequency) by time 819 ns <
Memory estimate: 944 bytes, allocs estimate: 2.
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
Range (min β¦ max): 3.958 ns β¦ 2.720 ΞΌs β GC (min β¦ max): 0.00% β¦ 99.41%
Time (median): 4.250 ns β GC (median): 0.00%
Time (mean Β± Ο): 4.884 ns Β± 29.187 ns β GC (mean Β± Ο): 7.69% Β± 1.40%
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
3.96 ns Histogram: frequency by time 7.04 ns <
Memory estimate: 16 bytes, allocs estimate: 1.
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
Range (min β¦ max): 1.958 ns β¦ 26.750 ns β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 2.000 ns β GC (median): 0.00%
Time (mean Β± Ο): 2.041 ns Β± 0.411 ns β GC (mean Β± Ο): 0.00% Β± 0.00%
β β β β β β
ββββββββββββββββββββ
ββββββββββββββ
ββ
ββββββ
βββ
βββββββββββββ β
1.96 ns Histogram: log(frequency) by time 2.83 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
"Benchmarking with datatype Float64"
BenchmarkTools.Trial: 10000 samples with 220 evaluations.
Range (min β¦ max): 333.900 ns β¦ 15.987 ΞΌs β GC (min β¦ max): 0.00% β¦ 96.78%
Time (median): 384.850 ns β GC (median): 0.00%
Time (mean Β± Ο): 455.342 ns Β± 466.676 ns β GC (mean Β± Ο): 14.45% Β± 13.43%
βββββ β
ββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββ
ββββ
β β
334 ns Histogram: log(frequency) by time 2.99 ΞΌs <
Memory estimate: 2.48 KiB, allocs estimate: 102.
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
Range (min β¦ max): 4.000 ns β¦ 3.191 ΞΌs β GC (min β¦ max): 0.00% β¦ 99.37%
Time (median): 4.542 ns β GC (median): 0.00%
Time (mean Β± Ο): 5.299 ns Β± 32.788 ns β GC (mean Β± Ο): 7.38% Β± 1.39%
ββββ
ββββββββ
ββ
ββ
ββββββββββββββββββββββββββββββββββββββββββββββ β
4 ns Histogram: frequency by time 9.71 ns <
Memory estimate: 16 bytes, allocs estimate: 1.
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
Range (min β¦ max): 1.917 ns β¦ 84.417 ns β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 2.083 ns β GC (median): 0.00%
Time (mean Β± Ο): 2.166 ns Β± 1.127 ns β GC (mean Β± Ο): 0.00% Β± 0.00%
β β β β
β β β β β β β β β
ββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββ β
1.92 ns Histogram: log(frequency) by time 3 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
"Benchmarking with datatype Rational{Int64}"
BenchmarkTools.Trial: 10000 samples with 208 evaluations.
Range (min β¦ max): 353.562 ns β¦ 21.655 ΞΌs β GC (min β¦ max): 0.00% β¦ 96.92%
Time (median): 413.260 ns β GC (median): 0.00%
Time (mean Β± Ο): 561.200 ns Β± 1.039 ΞΌs β GC (mean Β± Ο): 22.77% Β± 11.79%
ββ
β β
βββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββ β
354 ns Histogram: log(frequency) by time 7.81 ΞΌs <
Memory estimate: 4.05 KiB, allocs estimate: 102.
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
Range (min β¦ max): 4.000 ns β¦ 2.688 ΞΌs β GC (min β¦ max): 0.00% β¦ 99.32%
Time (median): 4.417 ns β GC (median): 0.00%
Time (mean Β± Ο): 4.956 ns Β± 28.274 ns β GC (mean Β± Ο): 7.15% Β± 1.39%
β
βββ
β
ββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββ β
4 ns Histogram: frequency by time 6.88 ns <
Memory estimate: 16 bytes, allocs estimate: 1.
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
Range (min β¦ max): 1.958 ns β¦ 37.666 ns β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 2.000 ns β GC (median): 0.00%
Time (mean Β± Ο): 2.037 ns Β± 0.473 ns β GC (mean Β± Ο): 0.00% Β± 0.00%
β β β β β β
ββββββββββββββββββββββββ
βββββ
βββ
ββββββ
βββββββββββββββ
βββββ β
1.96 ns Histogram: log(frequency) by time 2.79 ns <
Memory estimate: 0 bytes, allocs estimate: 0.