-
Notifications
You must be signed in to change notification settings - Fork 44
Closed
Description
A simple test show that structarray allocates more than array.
using BenchmarkTools
using StaticArrays
using StructArrays
n = 1000
positions = [SVector{3, Float64}(randn(3)) for _ in 1:n]
volumes = rand(n)
struct TestPoint
pos::SVector{3, Float64}
vol::Float64
end
struct TestBodyStruct
points::StructVector{TestPoint}
end
points_struct = StructVector{TestPoint}((positions, volumes))
body_struct = TestBodyStruct(points_struct)
struct TestBodyDirect
pos::Vector{SVector{3, Float64}}
vol::Vector{Float64}
end
body_direct = TestBodyDirect(positions, volumes)
function test_structarray_access_float(body, iterations=10000)
for _ in 1:iterations
for i in 1:length(body.points)
V = body.points.vol[i]
end
end
return nothing
end
function test_structarray_access_SV(body, iterations=10000)
for _ in 1:iterations
for i in 1:length(body.points)
P = body.points.pos[i]
end
end
return nothing
end
function test_direct_access_float(body, iterations=10000)
for _ in 1:iterations
for i in 1:length(body.pos)
V = body.vol[i]
end
end
return nothing
end
function test_direct_access_SV(body, iterations=10000)
for _ in 1:iterations
for i in 1:length(body.pos)
P = body.pos[i]
end
end
return nothing
end
println("\nStructArray getindex of Float64:")
@btime test_structarray_access_float($body_struct, 100)
println("\nArray getindex of Float64:")
@btime test_direct_access_float($body_direct, 100)
println("\nStructArray getindex of SVector:")
@btime test_structarray_access_SV($body_struct, 100)
println("\nArray getindex of SVector:")
@btime test_direct_access_SV($body_direct, 100)And I got the results like:
StructArray getindex of Float64:
9.265 ms (398100 allocations: 9.13 MiB)
Array getindex of Float64:
3.058 μs (0 allocations: 0 bytes)
StructArray getindex of SVector:
8.912 ms (398100 allocations: 10.65 MiB)
Array getindex of SVector:
1.900 ns (0 allocations: 0 bytes)Metadata
Metadata
Assignees
Labels
No labels