I the `cfunction` necessary here? Since you already have the return type `R`, wouldn't it suffice to annotate the call with a `::R` suffix? I am thinking:
immutable StableFunction{R,A} f StableFunction(f) = new(f) end call{R,A}(f::StableFunction{R,A}, x) = f(x)::R -erik > On Jan 20, 2015, at 17:56 , Jeff Bezanson <jeff.bezan...@gmail.com> wrote: > > Oh, I should also mention that the version that calls goo(foo()) takes > so little time because the entire loop is probably optimized out. > > On Tue, Jan 20, 2015 at 5:54 PM, Jeff Bezanson <jeff.bezan...@gmail.com> > wrote: >> Here is a hack that basically works by escaping through the C type system: >> >> ``` >> immutable CFunction{R,A} >> f >> p::Ptr{Void} >> CFunction(f) = new(f, cfunction(f, R, (A,))) >> end >> >> call{R,A}(f::CFunction{R,A}, x) = ccall(f.p, R, (A,), x) >> >> foo(x::Float64) = 0.0 >> goo(x::Float64) = x >> >> function test1() >> for i=1:100000000 >> f = foo >> r = f(1.0) >> goo(r) >> end >> end >> >> function test2() >> f = CFunction{Float64,Float64}(foo) >> for i=1:100000000 >> r = f(1.0) >> goo(r) >> end >> end >> ``` >> >> I added an argument to `foo` to increase the generality somewhat. >> test1() is the original test case. test2() is the new version. The >> CFunction object needs to be constructed outside the loop, but this >> can be stored in a data structure and reused anywhere. >> >> -Jeff >> >> >> On Tue, Jan 20, 2015 at 4:34 PM, Ivar Nesje <iva...@gmail.com> wrote: >>> Originally posted at https://github.com/JuliaLang/julia/issues/9863 -- Erik Schnetter <schnet...@gmail.com> http://www.perimeterinstitute.ca/personal/eschnetter/ My email is as private as my paper mail. I therefore support encrypting and signing email messages. Get my PGP key from https://sks-keyservers.net.
signature.asc
Description: Message signed with OpenPGP using GPGMail