Performance of functions returned from container

https://github.com/yuyichao/FunctionWrappers.jl

import FunctionWrappers: FunctionWrapper

using BenchmarkTools
const F64IntFunc = FunctionWrapper{Float64,Tuple{Int}}

const dict = Vector{F64IntFunc}(4);

const cdict = Int[1, 2, 3, 4]

f1(x::Int) = 1 + x
dict[1] = F64IntFunc(f1);

f2(x::Int) = 1 - x
dict[2] = F64IntFunc(f2);

f3(x::Int) = 1.0/x
dict[3] = F64IntFunc(f3);

f4(x::Int) = x << 1
dict[4] = F64IntFunc(f4);


ref = @benchmarkable begin
    a1, a2 = cdict[1], cdict[2]
    f1(a1), f2(a2)
end

test = @benchmarkable begin
    dict[1](1), dict[2](2)
end

run(ref)
run(test)
julia> run(ref)
BenchmarkTools.Trial: 
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     20.000 ns (0.00% GC)
  median time:      30.000 ns (0.00% GC)
  mean time:        30.841 ns (0.00% GC)
  maximum time:     11.913 μs (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> run(test)
BenchmarkTools.Trial: 
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     24.000 ns (0.00% GC)
  median time:      37.000 ns (0.00% GC)
  mean time:        40.765 ns (0.00% GC)
  maximum time:     11.749 μs (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1

Ran on 0.5. Seems to be some ambiguity problems on 0.6 but should be easily fixed in the package.

2 Likes