Hi all,
the @inbounds macro can speed up the in-place editing of a standard array by a factor of two; however it has no performance gain for the same task when applied on a sparse matrix. Please see my test functions and @btime benchmarks below. Why is this? Am I using @inbounds wrong on the sparse array / Is this feature not (yet) implemented? / Is there a reason it should not be implemented? I’m happy for any advice.
using BenchmarkTools
using SparseArrays
# test array
const testarr = [1.0, 2.0, 3.0]
# test sparse array
const testsparr = spzeros(3, 1)
testsparr[1, 1] = 1.0
testsparr[2, 1] = 2.0
testsparr[3, 1] = 3.0
# test function on standard array
function test_inbounds(testarr)
for i=1:50_000_000
testarr[2] += 2.0
end
testarr
end
function test_inbounds2(testarr)
for i=1:50_000_000
@inbounds testarr[2] += 2.0
end
testarr
end
# test functions on sparse array
function test_inbounds_sparse(testsparr)
for i=1:50_000_000
testsparr[2, 1] += 2.0
end
testsparr
end
function test_inbounds2_sparse(testsparr)
for i=1:50_000_000
@inbounds testsparr[2, 1] += 2.0
end
testsparr
end
function test_inbounds3_sparse(testsparr)
@inbounds for i=1:50_000_000
testsparr[2, 1] += 2.0
end
testsparr
end
benchmark results
@btime test_inbounds(testarr) # 111.919 ms (0 allocations: 0 bytes)
@btime test_inbounds2(testarr) # 48.900 ms (0 allocations: 0 bytes)
@btime test_inbounds_sparse(testsparr) # 551.772 ms (0 allocations: 0 bytes)
@btime test_inbounds2_sparse(testsparr) # 576.797 ms (0 allocations: 0 bytes)
@btime test_inbounds3_sparse(testsparr) # 581.419 ms (0 allocations: 0 bytes)
Best