Hi, I am trying to find out the best (fast & pretty) way to pass a bunch of 
parameters to a function. I checked the group posts and compiled some I 
found

const pnames = [:a, :b, :c, :d, :e]
macro unpack(ex)
  Expr(:block, [:($(pnames[i]) = $ex[$i]) for i = 1:length(pnames) ]...) |> 
esc
end

macro pack(ex)
    N = length(pnames)
    Expr(:block, [
:($ex = zeros($N,1)),
 [:( $ex[$i] = $(pnames[i]) ) for i = 1:length(pnames) ]
]...) |> esc
end

function test1(a,b,c,d,e)
    dx= a*b*c*d*e
    return dx
end;

function test2(f)
    a=f[1]
    b=f[2]
    c=f[3]
    d=f[4]
    e=f[5]
    dx= a*b*c*d*e
    return dx
end;

function test3(f)
    dx= f[1]*f[2]*f[3]*f[4]*f[5]
    return dx
end;

function test4()
    a=1
    b=2
    c=3
    d=4
    e=5
    dx= a*b*c*d*e
    return dx
end;

function test5()
    dx= 1*2*3*4*5
    return dx
end;

function test6(p)
    @unpack p
     dx= a*b*c*d*e
    return dx
end;

function test7()
     dx= aa*bb*cc*dd*ee
    return dx
end;

N=10000000
a=1; b=2; c=3; d=4; e=5;
f=[]
f=[1,2,3,4,5];
ff=[a,b,c,d,e];
@pack p
immutable Var
a::Float64
b::Float64
    c::Float64
    d::Float64
    e::Float64
end
s=Var(1.0,2.0,3.0,4.0,5.0)
global aa, bb, cc, dd, ee;
aa=1; bb=2; cc=3; dd=4; ee=5

@time for i in 1:N test1(a,b,c,d,e) end
@time for i in 1:N test2(f) end
@time for i in 1:N test3(f) end
@time for i in 1:N test2(ff) end
@time for i in 1:N test3(ff) end
@time for i in 1:N test4() end
@time for i in 1:N test5() end
@time for i in 1:N test6(p) end
@time for i in 1:N test7() end

Resulting in:

elapsed time: 1.885115751 seconds (639983704 bytes allocated)
elapsed time: 1.707763955 seconds (639983704 bytes allocated)
elapsed time: 2.829910887 seconds (639983704 bytes allocated)
elapsed time: 1.717995529 seconds (639983704 bytes allocated)
elapsed time: 2.803335021 seconds (639983688 bytes allocated)
elapsed time: 1.483327602 seconds (639983688 bytes allocated)
elapsed time: 1.468555675 seconds (639983688 bytes allocated)
elapsed time: 1.860042772 seconds (799983688 bytes allocated)
elapsed time: 1.904847681 seconds (639983704 bytes allocated)


Defining the value in the function seems fastest test5() and test4(), followed 
by reassigning within the function test2(). Next is passing all values 
test1(a,b,c,d,e) followed by using @pack macro aand global declaration (Do I 
understand correctly that @pack/@unpack rewrites the code before compiling?). 
Slowest is using indexing in an array (ugly too).


So my question is: any other ways that improve readability/speed?


Thanks

Reply via email to