packaging parameters for functions revisited

102 views
Skip to first unread message

Jon Norberg

unread,
May 30, 2014, 5:42:00 AM5/30/14
to julia...@googlegroups.com
There have been several posts about this, so I tried to compile what I could find to compare speed and pretty coding:


Best speed is assigning values or variables inside the function
Second best is reassigning parameters from array for each parameter. The @pack/@unpack macro also is pretty fast and much prettier
Third best is separate parameters and global assignment (longer function call code if many parameters or/and just no no to use global?)
Slowest is using indexed array as parameters in function (but this is ugly to read) and immutable types

So I am wondering, did I miss any that improve speed/prettiness?

Best, Jon



Mike Innes

unread,
May 30, 2014, 5:49:06 AM5/30/14
to julia...@googlegroups.com
You don't seem to have tried keyword arguments – these are usually the best option if you have a large set of parameters to pass a single function. They may not fit your use case but they might be interesting to benchmark at least.

Jon Norberg

unread,
May 30, 2014, 6:20:07 AM5/30/14
to julia...@googlegroups.com
Do you mean using a dict to pack/unpack them?

function f(x; args...)
    ###
end

where args is a dict?

Jon Norberg

unread,
May 30, 2014, 11:23:35 AM5/30/14
to julia...@googlegroups.com
I added the keyword version (not sure it is as intended though....)

function with_keyword(x::Float64=1.1; a::Float64=1.0,b::Float64=2.0,c::Float64=3.0,d::Float64=4.0,e::Float64=5.0)
    dx= a*b*c*d*e::Float64
end

Results now are: (slower due to being on my macbook and couldn't change in the ijulia book yet):

@time for i in 1:N with_keyword(1.1) end
@time for i in 1:N Brute() end
@time for i in 1:N assignInF() end
@time for i in 1:N assignFromArray(f) end
@time for i in 1:N assignFromArray(ff) end
@time for i in 1:N packUnpack(p) end
@time for i in 1:N sepParams(a,b,c,d,e) end
@time for i in 1:N globadAssign() end
@time for i in 1:N indexedArray(f) end
@time for i in 1:N immutableType(s) end
@time for i in 1:N indexedArray(ff) end
elapsed time: 1.859415076 seconds (639983688 bytes allocated)
elapsed time: 1.824780742 seconds (639983688 bytes allocated)
elapsed time: 1.815331646 seconds (639983688 bytes allocated)
elapsed time: 2.260640645 seconds (640031792 bytes allocated)
elapsed time: 2.491979295 seconds (800031648 bytes allocated)
elapsed time: 2.498344644 seconds (800030640 bytes allocated)
elapsed time: 2.478566736 seconds (800001228 bytes allocated)
elapsed time: 2.605157314 seconds (799983688 bytes allocated)
elapsed time: 2.365273473 seconds (640021904 bytes allocated)
elapsed time: 2.58554929 seconds (800004984 bytes allocated)
elapsed time: 2.520120133 seconds (800021760 bytes allocated)

Thus, keyword goes close to top, but not sure how to pass a,b,c,d,e to the function using keywords

cnbiz850

unread,
May 30, 2014, 5:02:08 PM5/30/14
to julia...@googlegroups.com
Thanks very much for the summary.

It is very interesting that assignFromArray is much faster than
indexedArray. Can anyone explain why?
Reply all
Reply to author
Forward
0 new messages