I tried that, but it was slower than my initial method of calling f().

I even tried called ccall directly, but it also didn't solve the problem of 
type instability (which makes me feel like I'm missing something 
important), and it had an additional problem of not accepting its type 
arguments as variables.

I'm going to try 0.4 tomorrow and see how it goes.

On Tuesday, January 20, 2015 at 10:06:40 PM UTC-6, Jeff Bezanson wrote:
>
> Oops, that's right, `call` only exists in 0.4. Probably the quickest 
> way to make this work in 0.3 is to write `call(f, x)` instead of 
> `f(x)` where needed. 
>
> On Tue, Jan 20, 2015 at 10:14 PM, Keith Mason <keith...@conning.com 
> <javascript:>> wrote: 
> > When I try this, I get an error: 
> > 
> > ERROR: type: test2: in apply, expected Function, got 
> > CFunction{Float64,Float64} 
> > 
> > 
> > It looks like "call" doesn't exist in version 0.3.  I suppose I need 0.4 
> to 
> > make this work.  It doesn't appear from the docs that call used to be 
> named 
> > something else. 
> > 
> > 
> > On Tuesday, January 20, 2015 at 4:54:08 PM UTC-6, Jeff Bezanson 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 
> > 
> > 
> > 
> > 
> > Please click here for important information regarding this e-mail 
> > communication. 
>

-- 


Please click here 
<http://www.e-disclaimer.com/conning/AD21D06B4CC99D2B4F24BA73FB4EED83.htm> for 
important information regarding this e-mail communication.

Reply via email to