Apparently it's 4~5x faster to do operations on Field.data
instead of Field
even thought I've inlined getindex
and setindex!
, not that it changes things much.
@inline getindex(f::Field, inds...) = getindex(f.data, inds...)
@inline setindex!(f::Field, v, inds...) = setindex!(f.data, v, inds...)
Probably just missing something simple but for now I'll use Field.data
. Would be nice to figure this out though.
g = RegularCartesianGrid((100, 100, 100), (10, 10, 10))
f1, f2 = CellField(g), FaceFieldX(g)
function δx1!(g::RegularCartesianGrid, f::CellField, δxf::FaceField)
for k in 1:g.Nz, j in 1:g.Ny, i in 1:g.Nx
@inbounds δxf[i, j, k] = f[i, j, k] - f[decmod1(i, g.Nx), j, k]
end
end
julia> @benchmark δx1!(g, f1, f2)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 4.542 ms (0.00% GC)
median time: 5.007 ms (0.00% GC)
mean time: 5.120 ms (0.00% GC)
maximum time: 11.010 ms (0.00% GC)
--------------
samples: 975
evals/sample: 1
function δx2!(g::RegularCartesianGrid, f::CellField, δxf::FaceField)
for k in 1:g.Nz, j in 1:g.Ny, i in 1:g.Nx
@inbounds δxf.data[i, j, k] = f.data[i, j, k] - f.data[decmod1(i, g.Nx), j, k]
end
end
julia> @benchmark δx2!(g, f2, f1)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 1.099 ms (0.00% GC)
median time: 1.198 ms (0.00% GC)
mean time: 1.253 ms (0.00% GC)
maximum time: 2.679 ms (0.00% GC)
--------------
samples: 3967
evals/sample: 1