Skip to content

Instantly share code, notes, and snippets.

@jiahao
Last active August 29, 2015 13:58
Show Gist options
  • Save jiahao/10399236 to your computer and use it in GitHub Desktop.
Save jiahao/10399236 to your computer and use it in GitHub Desktop.
Implement some statistics from 'Multiple Dispatch in Practice' in Julia
#Computes the dispatch ratio for each generic function defined in a module
#all: iterate over exported methods, or all methods?
function dispatchratio(M::Module=Base, all=false)
methodcount = Dict{Symbol, Int}()
for name in names(M, all)
try #name may not represent a function or function-able type
methodcount[name] = length(methods(eval(M, name)))
catch continue end
end
methodcount
end
const exported_functions_only = true
#Compute mean dispatch ratios by module
dr_ave = Dict{Symbol, (Float64, Int)}()
for name in names(Base, !exported_functions_only)
try #names(..., true) picks up some undefined symbols?!
obj = eval(Base, name)
if typeof(obj) == Module
dr = dispatchratio(obj, !exported_functions_only)
dr_ave[name] = length(dr)==0 ? (0.0, 0) : (mean(values(dr)), length(dr))
end
catch continue end
end
println("Mean dispatch ratios by module")
for name in sort([keys(dr_ave)...])
@printf("%20s: %8.4f (N = %4d)\n", name, dr_ave[name][1], dr_ave[name][2])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment