Method dispatch shootout

126 views
Skip to first unread message

felip...@gmail.com

unread,
Jul 28, 2015, 10:13:26 PM7/28/15
to julia-users
Maybe this is worth sharing. When considering using enums instead of types, I came up with 7 different ways to dispatch to methods, and these are the results of the performance tests.


Results:

julia> include("enum-vs-dispatch.jl")
enums with switch code
 323.515 milliseconds
Dispatch via Val Type
   1.068 seconds      (30000 k allocations: 458 MB, 1.96% gc time)
Dispatch via Const Val Type
 145.355 milliseconds
Dispatch via Type
 148.047 milliseconds
Vector of functions dispatch
 833.969 milliseconds (30000 k allocations: 458 MB, 2.14% gc time)
Functors
   1.011 seconds      (30000 k allocations: 458 MB, 1.62% gc time)
Const Functors
 147.741 milliseconds

Tim Holy

unread,
Jul 29, 2015, 6:54:48 AM7/29/15
to julia...@googlegroups.com
So there are 3 that are identical: "Const Val Type", "Type", and "Const
Functors." To veterans, this is not surprising.

What might be more surprising is that "Val Type" isn't fast. This is because
the type isn't inferable: the whole point of Val is to wrap things that aren't
types and turn them into a type, so the compiler doesn't know the type of
Val{EContinuous}. You can use "function barrier" techniques to work around
this kind of instability
(http://docs.julialang.org/en/latest/manual/performance-tips/#separate-kernel-functions), or (as you did) define consts.

Likewise, the "Functors" approach would work out mostly fine if instead of
timing at global scope you wrote your timings like this:

function time_functors(n, fc, fs, fe)
s = 0.0
for i = 1:n
s += fc(0.15, 2.0)
s += fs(0.15, 2.0)
s += fe(0.15, 2.0)
end
s
end

time_functors(1, fc, fs, fe)
@time time_functors(10000000, fc, fs, fe)

Accumulating into s prevents the compiler from noticing that you don't
actually use these values and elide the entire loop. (This doesn't always
happen without the s, but it's good defensive practice when timing things.)

(There's actually a small penalty for the non-const functors that I don't
immediately understand, but I lack time to follow it up right now.)

--Tim

On Tuesday, July 28, 2015 07:13:25 PM felip...@gmail.com wrote:
> Maybe this is worth sharing. When considering using enums instead of types,
> I came up with 7 different ways to dispatch to methods, and these are the
> results of the performance tests.
>
> The code is at: https://gist.github.com/felipenoris/660537161af33db34dcb
>
> *Results:*
>
> julia> include("enum-vs-dispatch.jl")
> enums with switch code
> 323.515 milliseconds
> Dispatch via Val Type
> 1.068 seconds (30000 k allocations: 458 MB, 1.96% gc time)
> Dispatch via Const Val Type
> 145.355 milliseconds
> *Dispatch via Type*
> * 148.047 milliseconds*

felip...@gmail.com

unread,
Jul 29, 2015, 7:09:28 AM7/29/15
to julia-users, tim....@gmail.com
Tim, this is really helpful. Thanks for taking your time!
Reply all
Reply to author
Forward
0 new messages