# \[ANN\] DynamicQuantities.jl: type stable physical quantities

**URL:** <https://discourse.julialang.org/t/ann-dynamicquantities-jl-type-stable-physical-quantities/99963>\
**Category:** Package Announcements\
**Tags:** package, announcement\
**Created:** [June 6, 2023, 8:07pm UTC](https://discourse.julialang.org/t/ann-dynamicquantities-jl-type-stable-physical-quantities/99963 "2023-06-06T20:07:08Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)\
**Post date:** [June 7, 2023, 1:06pm UTC](https://discourse.julialang.org/t/ann-dynamicquantities-jl-type-stable-physical-quantities/99963/7 "2023-06-07T13:06:49Z")

</div>

> [@non-Jedi](#):
>
> Interestingly, operations on heterogeneous arrays only have a 27x advantage for DynamicQuantities for `g`. I was expecting something more similar to the 1,500x gap for the non-inferred homogeneous case.

In general I think this would be best done with StructArrays.jl, so that the dimension-specific operations can be vectorized too. I’d be very curious to see how that changes your measurement.

Perhaps this is also because the dimensions are stored as `SimpleRatio{Int}` rather than an `Int8` (which is more amenable to vectorization)? @Oscar_Smith was suggesting this in the initial thread based on some measurements:

> [@Unitful.jl without parametrizing types on units?](https://discourse.julialang.org/t/unitful-jl-without-parametrizing-types-on-units/99869/21):
>
> 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%.

It’s likely that few people need more than an `Int8` for physical dimensions. The tricky part is dealing with integer overflows if we stick with `SimpleRatio`: even **four** repeated `+ (2//3) - (2//3)` would be enough to overflow `Int8`. (Since `SimpleRatio` doesn’t repeatedly divide by the gcd)

---

_[View the full topic](https://discourse.julialang.org/t/ann-dynamicquantities-jl-type-stable-physical-quantities/99963)._
