JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.43k stars 5.45k forks source link

Performance issue from generated code with quoted functions to call #3086

Closed toivoh closed 11 years ago

toivoh commented 11 years ago

The following test code

function codeit(fname, op)
    quote        
        function $fname(n)
            s = 0
            for i1 = 1:n
                s = $op(s, 1)
            end
            s
        end
    end
end

eval(codeit(:fsym, :-))
eval(codeit(:ffun, Expr(:quote, -)))

# warm up
fsym(1)
ffun(1)

n = 10_000_000
@time fsym(n)
@time ffun(n)

prints on my machine

elapsed time: 0.011305626 seconds
elapsed time: 0.641141447 seconds

The second incarnation of the function, ffun, is many orders of magnitude slower than fsym, while the only difference is that it refers to the operation by quoting the - function into the AST, instead of referring to it by name. I'm surprised at this, since I thought that using the name :- would only result in looking up the corresponding function.

toivoh commented 11 years ago

Thanks!