Skip to content

Commit a15fc35

Browse files
committed
Update timings
1 parent dd9c83d commit a15fc35

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

README.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ julia> unitful = convert(Unitful.Quantity, dyn_uni)
3030
julia> f(x, i) = x ^ i * 0.3;
3131

3232
julia> @btime f($dyn_uni, i) setup=(i=rand(1:10));
33-
87.755 ns (0 allocations: 0 bytes)
33+
10.719 ns (0 allocations: 0 bytes)
3434

3535
julia> @btime f($unitful, i) setup=(i=rand(1:10));
36-
30.708 μs (42 allocations: 1.91 KiB)
36+
30.583 μs (42 allocations: 1.91 KiB)
3737
```
3838

3939
**(Note the μ and n.)**
@@ -47,10 +47,10 @@ then you can get better speeds with Unitful:
4747
julia> g(x) = x ^ 2 * 0.3;
4848

4949
julia> @btime g($dyn_uni);
50-
85.063 ns (0 allocations: 0 bytes)
50+
10.051 ns (0 allocations: 0 bytes)
5151

5252
julia> @btime g($unitful);
53-
1.958 ns (0 allocations: 0 bytes)
53+
2.000 ns (0 allocations: 0 bytes)
5454
```
5555

5656
While both of these are type stable,
@@ -159,28 +159,34 @@ true
159159
## Types
160160

161161
Both the `Quantity`'s values and dimensions are of arbitrary type.
162-
For example, you might choose to use [Ratios.jl](https://github.com/timholy/Ratios.jl/)
163-
combined with [SaferIntegers.jl](https://github.com/JeffreySarnoff/SaferIntegers.jl)
164-
for faster dimension calculations:
162+
By default, dimensions are stored as a `DynamicQuantities.FixedRational{Int32,C}`
163+
object, which represents a rational number
164+
with a fixed denominator `C`. This is much faster than `Rational`.
165+
166+
However, for many applications, `FixedRational{Int8,6}` will suffice,
167+
and can be faster as it means the entire `Dimensions`
168+
struct will fit into 64 bits. Let's see an example:
165169

166170
```julia
167-
julia> using DynamicQuantities, Ratios, SaferIntegers
171+
julia> using DynamicQuantities; using DynamicQuantities: FixedRational
172+
173+
julia> R8 = FixedRational{Int8,6};
168174

169-
julia> Base.round(::Type{T}, x::SimpleRatio) where {T} = round(T, x.num // x.den) # Define missing function
175+
julia> R32 = FixedRational{Int32,2^4 * 3^2 * 5^2 * 7}; # Default
170176

171-
julia> q = Quantity(0.2, length=2)
177+
julia> q8 = [Quantity(randn(), R8, length=rand(-2:2)) for i in 1:1000];
172178
0.2 𝐋 ²
173179

174-
julia> q_fast = Quantity(Float32(0.2), SimpleRatio{SafeInt8}, length=2)
180+
julia> q32 = [Quantity(randn(), R32, length=rand(-2:2)) for i in 1:1000];
175181
0.2 𝐋 ²
176182

177-
julia> f(x, i) = x ^ i * 0.3;
183+
julia> f(x) = @. x ^ 2 * 0.5;
178184

179-
julia> @btime f($q, i) setup=(i=rand(1:10));
180-
83.117 ns (0 allocations: 0 bytes)
185+
julia> @btime f($q8);
186+
7.750 μs (1 allocation: 15.75 KiB)
181187

182-
julia> @btime f($q_fast, i) setup=(i=rand(1:10));
183-
10.594 ns (0 allocations: 0 bytes)
188+
julia> @btime f($q32);
189+
8.417 μs (2 allocations: 39.11 KiB)
184190
```
185191

186192
## Vectors

0 commit comments

Comments
 (0)