Hello.
I have a function of many variables, hermite_interpolation(t, t_array, f_array, df_array)
, and I want to use this function as the argument of another function, that I shall call do_something(pulse)
. However, the argument pulse
of the function do_something
must be a function of the time t
only. At first I was simply defining the function
pulse = t-> hermite_interpolation(t, t_array, filtered_f, filtered_df)
and feeding it to do_something
. Afterwards I learned that it was actually better to define
pulse2 = let t_array = t_array,
f_array = filtered_f,
df_array = filtered_df
t-> hermite_interpolation(t, t_array, f_array, df_array)
end
I benchmarked both functions, and I obtained
julia> @benchmark pulse($tf)
BenchmarkTools.Trial:
memory estimate: 48 bytes
allocs estimate: 3
--------------
minimum time: 80.339 ns (0.00% GC)
median time: 88.953 ns (0.00% GC)
mean time: 101.802 ns (6.52% GC)
maximum time: 17.307 μs (99.37% GC)
--------------
samples: 10000
evals/sample: 949
julia> @benchmark pulse2($tf)
BenchmarkTools.Trial:
memory estimate: 32 bytes
allocs estimate: 2
--------------
minimum time: 57.724 ns (0.00% GC)
median time: 64.536 ns (0.00% GC)
mean time: 73.016 ns (5.11% GC)
maximum time: 14.597 μs (99.49% GC)
--------------
samples: 10000
evals/sample: 973
I also benchmarked hermite_interpolation(t, t_array, f_array, df_array)
and I got
julia> @benchmark hermite_interpolation($tf, $t_array, $filtered_f, $filtered_df)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 29.318 ns (0.00% GC)
median time: 31.083 ns (0.00% GC)
mean time: 34.351 ns (0.00% GC)
maximum time: 666.528 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 995
Here we se that the original function hermite_interpolation(tf, t_array, filtered_f, filtered_df)
is still much faster than the closures pulse
and pulse2
.
My question is, is there a way to define faster closures? I would appreciate any help.