I have the following simplified example taken from real code that calculates a function over a difference for a vector of inputs and a function input
params = (a=2,b=3) #
calc(p::NamedTuple, t) = p.a*t^2+p.b*t^3 # Example function
vec=[1:1000;]
testbroad1(p::NamedTuple, vec::Vector, calc::Function, t) = calc.((p,), t .- vec)
function testbroad2(params::NamedTuple, vec::Vector, calc::Function, t)
c(t) = calc(params, t)
c.(t.-vec)
end
When testbroad1 and testbroad2 are benachmarked, the following happens
julia> @btime testbroad1($params, $vec, $calc, 5.0)
1.057 μs (5 allocations: 8.03 KiB)
julia> @btime testbroad2($params, $vec, $calc, 5.0)
922.944 ns (1 allocation: 7.94 KiB)
testbroad2 is faster by around 10%. In my more complex real world code the testbroad1 takes the double time to finish (not benchmarking but around 8 seconds instead of 4)
- Is that behavior expected or not?
- What is the best way to achieve the above? It is absolutely essential for me that
paramsand the functioncalcare passed as parameters totestbroadand the performance regression is not acceptable. Finding (the rather trivial) workaround has cost me couple of hours