I usually use reduce(∘, repeated(func, x))(a) when I need to apply func to a x times, but apparently it spends most of the time creating the single composed function; averaging ~20x as long as an equivalent accumulate in this example despite not needing any allocs/gc:
julia> f(a) = reduce(∘, repeated(exp, 1000))(a)
f (generic function with 1 method)
julia> @benchmark f(10.)
BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
Range (min … max): 132.725 μs … 844.193 μs ┊ GC (min … max): 0.00% … 0.00%
Time (median): 171.776 μs ┊ GC (median): 0.00%
Time (mean ± σ): 173.105 μs ± 40.335 μs ┊ GC (mean ± σ): 0.00% ± 0.00%
█▄ ▆
▂▂▁▂██▄▃▃▂▂▂█▆▅▄▂▂▂▂▂▂▂▂▁▁▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▂
133 μs Histogram: frequency by time 331 μs <
Memory estimate: 32 bytes, allocs estimate: 2.
julia> f(a) = accumulate((x,_)->exp(x), repeated(nothing, 1000); init=a)[end]
f (generic function with 1 method)
julia> @benchmark f(10.)
BenchmarkTools.Trial: 10000 samples with 4 evaluations per sample.
Range (min … max): 6.148 μs … 5.001 ms ┊ GC (min … max): 0.00% … 90.01%
Time (median): 7.973 μs ┊ GC (median): 0.00%
Time (mean ± σ): 8.881 μs ± 49.997 μs ┊ GC (mean ± σ): 5.07% ± 0.90%
▂ ▇▅▄▄▃▆█▆▆▄█▆▄▃▂▂▂▁▂▁▁▁▂▁▁ ▂
█▇▇▇▄███████████████████████████████▇██▇▇▇▇▆▅▆▆▆▆▆▆▅▆▅▆▄▄▄ █
6.15 μs Histogram: log(frequency) by time 14.9 μs <
Memory estimate: 7.85 KiB, allocs estimate: 2.
is there a way to tell julia to just apply the functions directly instead of first collecting them into a composed function?