using BenchmarkTools
function memoize_test(n_outputs)
function foo_i(i::Int64, u...)
end
return [(u...) -> foo_i(i, u...) for i in 1:n_outputs]
end
fun = memoize_test(10)
u_ = rand(28)
@btime $fun[1]($(u_)...)
Result:
1.560 ΞΌs (59 allocations: 1.14 KiB)
Why does it make some allocation?
If you could give me some tips, thank you very much.
Great. Thank you very much.
However, when I use the package TimerOutputs, why does the result show that there are some allocations?
using TimerOutputs
function test_time()
u = rand(28)
u_ = Tuple(u)
fun = memoize_test(10)
reset_timer!()
for i = 1:10
@timeit "fun" fun[1](u_...)
end
show(TimerOutputs.get_defaulttimer());
end
test_time()
Result:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Time Allocations
βββββββββββββββββββββββ ββββββββββββββββββββββββ
Tot / % measured: 28.3ΞΌs / 73.1% 12.2KiB / 93.2%
Section ncalls time %tot avg alloc %tot avg
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
fun 10 20.7ΞΌs 100.0% 2.07ΞΌs 11.4KiB 100.0% 1.14KiB
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Some allocations would arise from allocating the vector of functions. If the lengths are known to be constant, perhaps you may use Tuples, or StaticArrays