Sorry, but I don’t quite get what you mean:
While Base.Math.@horner(5, 1, 2, 3, 4)
just runs the macro and gives me 586
pretyping @macroexpand then tells me (Base.Math.muladd)
What does this tell me?
That’s a problem with the syntax highlighting. It’s giving you (Base.Math.muladd)(#19#t, (Base.Math.muladd)(#19#t, (Base.Math.muladd)(#19#t, 4, 3), 2), 1)
which is the code that the macro is producing.
macro horner01(x, p...)
ex = p[end]
for i = length(p)-1:-1:1
ex = :($(p[i]) + $x * $ex)
end
ex
end
function horner02(x, p::Array)
ex = p[end]
for i = length(p)-1:-1:1
ex = ((p[i]) + x * ex)
end
ex
end
setprecision(10^8)
x=big(9528)/10000
@time @horner01(x,4,3,2,1);
@time horner02(x,[4,3,2,1]);
So here it is not faster…Why should it be faster?
BTW: if I change function to macro it does not like this p::Array, why?
Also it is telling me
WARNING: Method definition @horner01(ANY<:Any, Any…) in module Main at C:\Users\Diger\Documents\Julia\various math problems\compare horner.jl:2 overwritten at C:\Users\Diger\Documents\Julia\various math problems\compare horner.jl:2.
WARNING: Method definition horner02(Any, Array{Int64, 1}) in module Main at C:\Users\Diger\Documents\Julia\various math problems\compare horner.jl:10 overwritten at C:\Users\Diger\Documents\Julia\various math problems\compare horner.jl:10.
julia> using BenchmarkTools
julia> f(x) = Base.Math.@horner(x, 4, 3, 2, 1)
f (generic function with 1 method)
julia> @btime f(0.2)
1.847 ns (0 allocations: 0 bytes)
4.688
julia> v = [4., 3., 2., 1.]
4-element Array{Float64,1}:
4.0
3.0
2.0
1.0
julia> function horner02(x, p::Array)
ex = p[end]
for i = length(p)-1:-1:1
ex = ((p[i]) + x * ex)
end
ex
end
horner02 (generic function with 1 method)
julia> @btime horner02(0.2, $v)
12.850 ns (0 allocations: 0 bytes)
4.688