EDIT: added length 500 and also MutableSmallVector
I’ve run some benchmarks (with Julia 1.11.5). Here are examples for lengths 32 and 500 with Int16
elements. The timings are for 1000 operations of the given kind, numbers in brackets indicate allocations.
It appears that FixedSizeVector
is noticeably faster than Vector
for addition and much faster for in-place addition and comparisons. (For both kinds of additions the differences are much smaller or disappear for length 500.) However, FixedSizeVector
is somewhat slower for sum
and count
and much slower when comparing two equal vectors. Creating a FixedSizeVector
from a iterator is also slow (if I’ve done that correctly).
Length <= 32 (or == 32 if not possible otherwise)
type | create vec | create itr | sum |
v + w |
v .+= w |
count(>(c), v) |
v == w |
v == w max |
---|---|---|---|---|---|---|---|---|
Vector{Int16} |
31.259 μs (2) | 23.356 μs (2) | 7.509 μs | 43.406 μs (2) | 21.584 μs | 11.049 μs | 6.174 μs | 7.009 μs |
FixedSizeVectorDefault{Int16} |
24.222 μs (1) | 72.832 μs (1) | 14.467 μs | 29.580 μs (1) | 8.780 μs | 12.652 μs | 3.224 μs | 23.179 μs |
MVector{32, Int16} |
10.568 μs (1) | 9.750 μs (1) | 2.347 μs | 5.875 μs | 21.765 μs | 2.758 μs | 3.329 μs | 15.119 μs |
MutableFixedVector{32, Int16} |
9.698 μs (1) | 8.549 μs (1) | 3.085 μs | 2.821 μs | 4.490 μs | 1.594 μs | 2.487 μs | 2.719 μs |
MutableSmallVector{32, Int16} |
19.352 μs (1) | 10.478 μs (1) | 3.057 μs | 3.157 μs | 4.512 μs | 2.020 μs | 2.679 μs | 3.229 μs |
Length <= 500 (or == 500 if not possible otherwise)
type | create vec | create itr | sum |
v + w |
v .+= w |
count(>(c), v) |
v == w |
v == w max |
---|---|---|---|---|---|---|---|---|
Vector{Int16} |
61.644 μs (2) | 69.742 μs (2) | 39.312 μs | 102.721 μs (2) | 123.097 μs | 46.504 μs | 6.287 μs | 30.817 μs |
FixedSizeVectorDefault{Int16} |
82.601 μs (1) | 655.020 μs (1) | 47.316 μs | 100.777 μs (1) | 93.896 μs | 59.326 μs | 3.907 μs | 342.495 μs |
MVector{500, Int16} |
81.885 μs (1) | 65.354 μs (1) | 19.550 μs | 104.291 μs | 367.615 μs | 21.174 μs | 4.126 μs | 198.523 μs |
MutableFixedVector{500, Int16} |
89.185 μs (1) | 90.337 μs (1) | 57.662 μs | 95.209 μs | 117.661 μs | 22.247 μs | 40.584 μs | 53.424 μs |
MutableSmallVector{500, Int16} |
188.431 μs (1) | 80.513 μs (1) | 123.028 μs | 114.714 μs | 138.296 μs | 29.382 μs | 69.503 μs | 69.516 μs |
create vec = create from Vector
create itr = create from iterator (using collect_as
for FixedSizeVector
)
v == w
max = comparing two equal vectors
Note: sum
for MVector
does not widen the element type to Int
. The sum of two MVector
s is an MVector
by default; I’ve converted it to SVector
.
The benchmark code is benchmark/benchmark_vec.jl
in the SmallCollections.jl repository.