Now I understand. Yes, the problem is you're running the operations in global scope. If you really want to go this way, then you'll probably have to make your macro create a function and then call it.
--Tim On Thursday, March 12, 2015 09:41:18 AM Johan Sigfrids wrote: > But the compile time shouldn't be very big, should it? For some bigger data > set and more complex computation the compile time should add pretty > insignificantly to the running time. The case I'm running into is something > like this: > > function test(a, b, c) > @map(sqrt(a^2 + b^2) + c, a, b) > end > a = randn(10_000_000) > b = randn(10_000_000) > c = randn() > @time test1(a, b ,c) > @time @map(sqrt(a^2 + b^2) + c, a, b) > > elapsed time: 0.096866291 seconds (80004616 bytes allocated) > > elapsed time: 4.713548649 seconds (1839991368 bytes allocated, 20.06% gc > time) > > I wonder if I could do something to get closer the the function case > performance in the global case. > On Thursday, March 12, 2015 at 5:07:57 AM UTC+2, Tim Holy wrote: > > If your _usage_ of @map is in the global scope, then it has to compile the > > expressions each time you use it. That may or may not be a problem for > > you. > > > > As an alternative that only requires compilation on the first call, try > > FastAnonymous or NumericFuns. > > > > --Tim > > > > On Wednesday, March 11, 2015 02:25:10 PM Johan Sigfrids wrote: > > > I've been playing around with creating a @map macro: > > > > > > indexify(s::Symbol, i, syms) = s in syms ? Expr(:ref, s, i) : s > > > indexify(e::Expr, i, syms) = Expr(e.head, e.args[1], [indexify(a, i, > > > > syms) > > > > > for a in e.args[2:end]]...) > > > indexify(a::Any, i, syms) = a > > > macro map(expr, args...) > > > > > > quote > > > > > > @assert all([map(length, ($(args...),))...] .== > > > > length($(args[1]))) > > > > > out = Array(typeof($(indexify(expr, 1, args))), > > > > size($(args[1]))) > > > > > for i in 1:length(out) > > > > > > @inbounds out[i] = $(indexify(expr, :i, args)) > > > > > > end > > > out > > > > > > end > > > > > > end > > > > > > When used inside a function it is nice and fast (around 40x faster than > > > map), but when used in global scope is is twice as slow as map. I assume > > > this is because of global variables prevent optimization. Now I wonder > > > > if > > > > > there is some way to introduce a scope to make the variables in expr > > > > local?
