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 <[email protected]> wrote:
> Originally posted at https://github.com/JuliaLang/julia/issues/9863

Reply via email to