There is no performance penalty for anonymous functions at all (in fact, they are internally handled by the same mechanisms that handle “regular” functions). It’s just that you’re seeing measurements which are not particularly helpful because:
- Your
f
field is un-typed (as Chris mentioned). See: https://docs.julialang.org/en/stable/manual/performance-tips/#Avoid-fields-with-abstract-type-1 - You are timing in global scope. For accurate, representative benchmark results, especially for fast functions, use GitHub - JuliaCI/BenchmarkTools.jl: A benchmarking framework for the Julia language, e.g.
@benchmark process_x2(1.0, $myt2, 2.0)
.