So the "call" syntax on a type works, but using the implementation
referenced above, it is SLOW.
# Slow, function callback
function test1(f, n)
summ=0
for i=1:n
summ=summ+f(i)
end
return summ
end
g(i::Int)=i
# Fast, hardwired function g()
function test2(n)
summ=0
for i=1:n
summ=summ+g(i)
end
return summ
end
# Fast, using method defined for an object
function test4{T}(o::T, n)
summ=0
for i=1:n
summ=summ+get(o,i)
end
return summ
end
type MyFun1
end
function get(o::MyFun1,i::Int)
return g(i)
end
# Slow, using "call" on a singleton
type MyFun3 end
call(::Type{MyFun3},i::Int)=g(i)::Int
n=10002345;
@time println("test1(g, n) = $( test1(g, n) )")
@time println("test2(n) = $( test2(n) )")
@time println("test4(MyFun1(), n) = $( test4(MyFun1(), n) )")
@time println("test1(MyFun3, n) = $( test1(MyFun3, n) )")
julia> include("fun.jl")
test1(g, n) = 50023457750685
elapsed time: 0.610016329 seconds (480173520 bytes allocated, 27.80% gc
time)
test2(n) = 50023457750685
elapsed time: 0.003086749 seconds (75304 bytes allocated)
test4(MyFun1(), n) = 50023457750685
elapsed time: 0.004098207 seconds (90436 bytes allocated)
test1(MyFun3, n) = 50023457750685
elapsed time: 0.665630908 seconds (480184232 bytes allocated, 23.21% gc
time)