That macro being slow at the top level isn't really a strike against the 
macro technique, because it's easily resolved:

(Although oddly enough, a let binding doesn't really help here – anyone 
know why?)

macro sumf(f, xs)
  quote
    function inner(s = 0.0, x = $(esc(xs)))
      for i = 1:length(x)
        s += $(esc(f))(x[i])
      end
      s
    end
    inner()
  end
end

Although you're right that if you call this from a function which itself 
takes f as a parameter, you'll lose the speed boost again (technically you 
could resolve this by making the calling function a macro in the same way, 
but that's equally awful).

Hopefully this kind of inlining will be automatic soon, so we won't have to 
resort to ugly language hacks to make things fast; on the other hand, it's 
kinda cool that we *can* resort to ugly language hacks to make things fast 
:)


On Saturday, 17 May 2014 16:18:15 UTC+1, Tim Holy wrote:
>
> If you want to make that fast, you need to wrap that inside a function, 
> using 
> a separate name for each user-supplied f. Example: 
>
> function sumf_with_sinc_plus_x(xs) 
>     @sumf(sinc_plus_x, xs) 
> end 
>
> function sumf_with_exp(xs) 
>     @sumf(exp, xs) 
> end 
>
> If you don't wrap it in a function, then it runs in global scope, and 
> that's 
> horribly slow. 
>
> My version allows you to pass a function as an argument, and mimics what 
> it 
> would be like if we could pass functions-as-arguments without a 
> performance 
> penalty. 
>
> --Tim 
>
> On Saturday, May 17, 2014 05:03:07 AM Mike Innes wrote: 
> > I may be missing the point here, but wouldn't it be easier to define 
> sumf 
> > as a macro? 
> > 
> > macro sumf(f, xs) 
> >   quote 
> >     s = 0.0 
> >     x = $(esc(xs)) 
> >     for i = 1:length(x) 
> >       s += $(esc(f))(x[i]) 
> >     end 
> >     s 
> >   end 
> > end 
> > 
> > @sumf(sinc_plus_x, x) 
> > 
> > This is just as fast and has the advantage that it will work when f is 
> only 
> > in the local scope. 
> > 
> > On Saturday, 17 May 2014 11:50:56 UTC+1, Tim Holy wrote: 
> > > On Friday, May 16, 2014 02:36:03 PM francoi...@gmail.com 
> <javascript:>wrote: 
> > > > - The solver need to be fast and for that, inlining is of paramount 
> > > > importance. I know that there is no way to inline F for the time 
> being. 
> > > 
> > > Do 
> > > 
> > > > we expect inlining on function argument in the near future of Julia 
> ? 
> > > 
> > > I can't speak for when this will happen in a "nice" way, but using 
> Julia's 
> > > metaprogramming capabilities there is a (somewhat ugly) way to get 
> what 
> > > you 
> > > want. Since I'm planning to use this trick myself shortly, I created a 
> > > little 
> > > demonstration: 
> > > 
> > > https://gist.github.com/timholy/bdcee95f9b7725214d8b 
> > > 
> > > If you prefer, you don't have to separate out the definition of the 
> body 
> > > from 
> > > the eval(quote...end) part, I just did it that way to better 
> illustrate 
> > > the 
> > > separate ideas. 
> > > 
> > > --Tim 
>

Reply via email to