made me wonder if a vector of vectors would be faster than a vector of struct for this case where the struct only contains Int. In fact it was much slower, but is there a better way to write double! and double_array! ?
using BenchmarkTools
mutable struct MutFoo
a::Int
b::Int
end
function double_b!(v::Vector{MutFoo})
for w in v
w.b *= 2
end
v
end
struct ImmutFoo
a::Int
b::Int
end
function double_b!(v::Vector{ImmutFoo})
for i in eachindex(v)
# @show i,v[i].a,v[i].b
v[i] = ImmutFoo(v[i].a, v[i].b * 2)
end
return
end
function double!(v::Vector{Vector{Int64}},size::Int64)
@inbounds for i in 1:size
# @show i,v[i].a,v[i].b
v[i][2] *= 2
end
return
end
function double_array!(v::Array{Int64,2},size::Int64)
@inbounds for i in 1:size
# @show i,v[i].a,v[i].b
v[2,i] *= 2
end
# v[2,1:size] .= @views v[2,1:size] * 2
return
end
size = 10^6
println("Benchmarking vector of mutable objects")
const mut_vec = [MutFoo(1, 1) for i in 1:size]
# @show mut_vec
@btime double_b!(mut_vec)
# @show mut_vec
println("Benchmarking vector of immutable objects")
const immut_vec = [ImmutFoo(0, 0) for i in 1:size]
@btime double_b!(immut_vec)
println("Benchmarking vector of vectors")
vec = Vector{Vector{Int64}}(undef,size)
for i in eachindex(vec)
vec[i] = Vector{Int64}(undef,2)
vec[i][1:2] .= 1,1
end
@btime double!(vec,size)
println("Benchmarking array")
arr = Array{Int64,2}(undef,2,size)
for (i,v) in enumerate(eachcol(arr))
arr[:,i] .= 1,1
end
@btime double_array!(arr,size)
with the following results:
Benchmarking vector of mutable objects
WARNING: redefinition of constant Main.mut_vec. This may fail, cause incorrect answers, or produce other errors.
1.715 ms (0 allocations: 0 bytes)
Benchmarking vector of immutable objects
WARNING: redefinition of constant Main.immut_vec. This may fail, cause incorrect answers, or produce other errors.
183.700 ÎĽs (0 allocations: 0 bytes)
Benchmarking vector of vectors
5.547 ms (0 allocations: 0 bytes)
Benchmarking array
179.200 ÎĽs (0 allocations: 0 bytes)