Unitful.jl without parametrizing types on units?

Using @btime randn(5) .* Dimensions(mass=2/5, length=2) as a benchmark, I see a pretty big improvement going to Int8 instead of Int. It won’t make much difference for scalars, but for vectors, it cuts allocations by 75% and time by 19%.

#before:
julia> @btime randn(500) .* Dimensions(mass=2/5, length=2);
  17.086 μs (1012 allocations: 145.22 KiB)
# after:
julia> @btime randn(500) .* Dimensions(mass=2/5, length=2);
  13.984 μs (1011 allocations: 39.77 KiB)
1 Like

Is there a way to do type-stable units without constraining the dimensions to a fixed set? I like to create dimensions like NumberOfBooks and calculate

(10 book / shelf) * (8 shelf / case) * 3 case

with little syntactic overhead.

That would require a Dict rather than a struct and would as a result be a good bit slower.

1 Like

Very nice!


Thanks for the comments everyone. I put up a new thread specifically on the package here: [ANN] DynamicQuantities.jl: type stable physical quantities

1 Like

Any chance we can get a moderator to move the messages about DynamicQuantities.jl to the new thread?

1 Like