Open lmiq opened 3 years ago
julia> using StaticArrays, FastPow, BenchmarkTools julia> function forces!(x,f,d,idxs) @inbounds for id in axes(idxs,1) i = idxs[id,1] j = idxs[id,2] r = x[j] - x[i] @fastpow dudr = -12*(1/d^7 - 1/d^4)*r f[i] = f[i] + dudr f[j] = f[j] - dudr end return f end forces! (generic function with 1 method) julia> @benchmark forces!(x,f,d,idxs) setup = ( x = [ rand(SVector{3,Float64}) for _ in 1:1000 ]; f = [ zeros(SVector{3,Float64}) for _ in 1:1000 ]; d = rand(); idxs = rand(1:1000,200,2) ) BenchmarkTools.Trial: 10000 samples with 196 evaluations. Range (min … max): 468.296 ns … 2.371 μs ┊ GC (min … max): 0.00% … 0.00% Time (median): 485.852 ns ┊ GC (median): 0.00% Time (mean ± σ): 506.027 ns ± 68.256 ns ┊ GC (mean ± σ): 0.00% ± 0.00% ▄▇█▇▆▅▅▄▃▃▃▂▂▁▁▁▁▁ ▂ ████████████████████████▆█▇▇▇▇▅▆▆▇▇█▇█▇▇▆▇▇▆▇▆▆▆▆▄▅▅▆▅▅▄▅▄▄▄ █ 468 ns Histogram: log(frequency) by time 774 ns < Memory estimate: 0 bytes, allocs estimate: 0. julia> @benchmark forces!(x,f,d,idxs) setup = ( x = [ rand(SVector{4,Float64}) for _ in 1:1000 ]; f = [ zeros(SVector{4,Float64}) for _ in 1:1000 ]; d = rand(); idxs = rand(1:1000,200,2) ) BenchmarkTools.Trial: 10000 samples with 310 evaluations. Range (min … max): 259.797 ns … 1.496 μs ┊ GC (min … max): 0.00% … 0.00% Time (median): 296.848 ns ┊ GC (median): 0.00% Time (mean ± σ): 313.750 ns ± 55.203 ns ┊ GC (mean ± σ): 0.00% ± 0.00% ▆█▇▄▃▁ ▁▂████████▇▆▆▅▄▄▃▄▃▃▂▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▂ 260 ns Histogram: frequency by time 550 ns < Memory estimate: 0 bytes, allocs es