# Unitful.jl without parametrizing types on units?

**URL:** <https://discourse.julialang.org/t/unitful-jl-without-parametrizing-types-on-units/99869>\
**Category:** General Usage\
**Tags:** question, unitful\
**Created:** [June 5, 2023, 5:08am UTC](https://discourse.julialang.org/t/unitful-jl-without-parametrizing-types-on-units/99869 "2023-06-05T05:08:41Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 6, 2023, 5:49pm UTC](https://discourse.julialang.org/t/unitful-jl-without-parametrizing-types-on-units/99869/21 "2023-06-06T17:49:20Z")

</div>

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%.

```julia
#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)

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [June 6, 2023, 6:07pm UTC](https://discourse.julialang.org/t/unitful-jl-without-parametrizing-types-on-units/99869/22 "2023-06-06T18:07:40Z")

</div>

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

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

```

with little syntactic overhead.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 6, 2023, 6:13pm UTC](https://discourse.julialang.org/t/unitful-jl-without-parametrizing-types-on-units/99869/23 "2023-06-06T18:13:01Z")

</div>

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

---

<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 6, 2023, 8:11pm UTC](https://discourse.julialang.org/t/unitful-jl-without-parametrizing-types-on-units/99869/24 "2023-06-06T20:11:05Z")

</div>

> [@Oscar\_Smith](#):
>
> cuts allocations by 75% and time by 19%.

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](https://discourse.julialang.org/t/ann-dynamicquantities-jl-type-stable-physical-quantities/99963)

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 6, 2023, 8:27pm UTC](https://discourse.julialang.org/t/unitful-jl-without-parametrizing-types-on-units/99869/25 "2023-06-06T20:27:05Z")

</div>

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

[Previous page](https://discourse.julialang.org/t/unitful-jl-without-parametrizing-types-on-units/99869.md?page=1)
