Skip to content

Instantly share code, notes, and snippets.

@matbesancon
Last active August 20, 2019 09:12
Show Gist options
  • Save matbesancon/69fca8afdba2a65fc3a539cf18863a13 to your computer and use it in GitHub Desktop.
Save matbesancon/69fca8afdba2a65fc3a539cf18863a13 to your computer and use it in GitHub Desktop.
setindex boundchecked
julia> function setindex(x::Tuple, v, i::Integer)
Base.@_inline_meta
@boundscheck 1 <= i <= length(x) || throw(BoundsError(x, i))
Base._setindex(v, i, x...)
end
julia> using BenchmarkTools
julia> const t1 = (42, "", π)
(42, "", π = 3.1415926535897...)
julia> const t2 = (3,)
(3,)
julia> const t3 = Tuple(rand(20))
(0.12092059089915685, 0.03661674260098535, 0.4387447959228026, 0.24924518824102404, 0.8030810236448274, 0.6113071436338902, 0.1478887008495069, 0.05252965682680899, 0.7149387429707006, 0.6413502899926891, 0.019950378256176515, 0.6224574656550699, 0.1629190889632346, 0.5944608345610098, 0.7373917235238237, 0.3132544007992286, 0.8921873321120846, 0.7619310527544245, 0.12834641320033335, 0.8689002074555439)
julia> for tup in (t1, t2, t3)
for i in eachindex(tup)
@btime Base.setindex($tup, 3//2, $i)
@btime setindex($tup, 3//2, $i)
println("-----------------------")
end
end
139.912 ns (2 allocations: 64 bytes)
142.343 ns (2 allocations: 64 bytes)
-----------------------
159.649 ns (2 allocations: 64 bytes)
151.078 ns (2 allocations: 64 bytes)
-----------------------
153.704 ns (2 allocations: 80 bytes)
161.039 ns (2 allocations: 80 bytes)
-----------------------
111.920 ns (2 allocations: 64 bytes)
111.499 ns (2 allocations: 64 bytes)
-----------------------
472.692 ns (21 allocations: 512 bytes)
453.736 ns (21 allocations: 512 bytes)
-----------------------
482.299 ns (21 allocations: 512 bytes)
448.128 ns (21 allocations: 512 bytes)
-----------------------
480.917 ns (21 allocations: 512 bytes)
458.658 ns (21 allocations: 512 bytes)
-----------------------
487.662 ns (21 allocations: 512 bytes)
462.497 ns (21 allocations: 512 bytes)
-----------------------
490.397 ns (21 allocations: 512 bytes)
461.269 ns (21 allocations: 512 bytes)
-----------------------
492.378 ns (21 allocations: 512 bytes)
456.903 ns (21 allocations: 512 bytes)
-----------------------
496.508 ns (21 allocations: 512 bytes)
469.423 ns (21 allocations: 512 bytes)
-----------------------
497.560 ns (21 allocations: 512 bytes)
469.742 ns (21 allocations: 512 bytes)
-----------------------
489.479 ns (21 allocations: 512 bytes)
461.036 ns (21 allocations: 512 bytes)
-----------------------
481.549 ns (21 allocations: 512 bytes)
456.985 ns (21 allocations: 512 bytes)
-----------------------
489.877 ns (21 allocations: 512 bytes)
464.812 ns (21 allocations: 512 bytes)
-----------------------
492.149 ns (21 allocations: 512 bytes)
467.533 ns (21 allocations: 512 bytes)
-----------------------
497.595 ns (21 allocations: 512 bytes)
475.567 ns (21 allocations: 512 bytes)
-----------------------
484.804 ns (21 allocations: 512 bytes)
464.026 ns (21 allocations: 512 bytes)
-----------------------
528.773 ns (21 allocations: 512 bytes)
485.636 ns (21 allocations: 512 bytes)
-----------------------
522.938 ns (21 allocations: 512 bytes)
499.346 ns (21 allocations: 512 bytes)
-----------------------
506.918 ns (21 allocations: 512 bytes)
482.187 ns (21 allocations: 512 bytes)
-----------------------
491.385 ns (21 allocations: 512 bytes)
473.790 ns (21 allocations: 512 bytes)
-----------------------
520.421 ns (21 allocations: 512 bytes)
499.918 ns (21 allocations: 512 bytes)
-----------------------
519.526 ns (21 allocations: 512 bytes)
481.840 ns (21 allocations: 512 bytes)
-----------------------
julia> function f(x::Tuple, v)
@info "Normal setindex"
@btime for i in eachindex($x)
r = Base.setindex($x, $v, i)
@assert r[end] != 33
end
@info "New setindex"
@btime for i in eachindex($x)
r = setindex($x, $v, i)
@assert r[end] != 33
end
end
julia> function f2(x::Tuple, v)
@info "Normal setindex"
@btime for i in eachindex($x)
r = Base.setindex($x, $v, i)
@assert r[end] != 33
end
@info "New setindex"
@btime @inbounds for i in eachindex($x)
r = setindex($x, $v, i)
@assert r[end] != 33
end
end
julia> f2((3, 4, 7, 9, 8, 7, 5, 4, 1, 3, 4, 3, 55), 22)
[ Info: Normal setindex
7.466 ns (0 allocations: 0 bytes)
[ Info: New setindex
8.756 ns (0 allocations: 0 bytes)
julia> f((3, 4, 7, 9, 8, 7, 5, 4, 1, 3, 4, 3, 55), 22)
[ Info: Normal setindex
7.734 ns (0 allocations: 0 bytes)
[ Info: New setindex
7.467 ns (0 allocations: 0 bytes)
julia> f((3, 4, 7, 9, 8, 7, 5, 4, 1, 3, 4, 3, 55), 22)
[ Info: Normal setindex
8.762 ns (0 allocations: 0 bytes)
[ Info: New setindex
7.467 ns (0 allocations: 0 bytes)
julia> f((3, 4, 7, 9, 8, 7, 5, 4, 1, 3, 4, 3, 55), "ha")
[ Info: Normal setindex
2.756 ns (0 allocations: 0 bytes)
[ Info: New setindex
11.598 ns (0 allocations: 0 bytes)
julia> f2((3, 4, 7, 9, 8, 7, 5, 4, 1, 3, 4, 3, 55), "ha")
[ Info: Normal setindex
2.553 ns (0 allocations: 0 bytes)
[ Info: New setindex
2.842 ns (0 allocations: 0 bytes)
julia> f((3, 4, 7, 9, 8, 7, 5, 4, 1, 3, 4, 3, 55), π)
[ Info: Normal setindex
2.216 ns (0 allocations: 0 bytes)
[ Info: New setindex
2.217 ns (0 allocations: 0 bytes)
julia> f2((3, 4, 7, 9, 8, 7, 5, 4, 1, 3, 4, 3, 55), π)
[ Info: Normal setindex
2.216 ns (0 allocations: 0 bytes)
[ Info: New setindex
2.290 ns (0 allocations: 0 bytes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment