I am encountering slower than hoped for performance in my usage of Julia (in comparison to Matlab). I am sure that my coding style is largely responsible for this, but would appreciate any pointers.
I am calculating the probability of observing a given set (conditional on some values and a cut-off). I have tried to abstract away from the problem as much as possible and give a MWE that’s as minimal as possible. I’ve annotated to provide some insights on what I doing. The Julia profiler and Matlab profiler differ a lot, up to the point that I find it very hard to navigate any output the profiler gives me (I’m not a programmer, just an applied user).
using Combinatorics a = 5; values = randn(5, 15, 500) sets = collect(powerset(1:a)) deleteat!(sets,1) inv_set = collect(1:a) p_set = zeros(size(sets,1), 15, 500) @time for Z in 1:size(sets,1) current_set = vcat(hcat(sets[Z,:]...)...); # get the vector with the current set inv_current_set = setdiff(inv_set,hcat(sets[Z,:]...)); # get the vector with inverse of current set first_part = prod(1 .-exp.(-exp.(-1 .*(0 .-values[current_set,:,:]))), dims = 1); # calculate p current set second_part = prod(exp.(-exp.(-1 .*(0 .-values[inv_current_set,:,:]))), dims = 1); # calculate p inv set third_part = prod(exp.(-exp.(-1 .*(0 .-values[:,:,:]))), dims = 1); # calculate p empty set p_set[Z,:,:] = (first_part.*second_part) ./ (1 .- third_part); # wrap it all up end
Julia Version 1.2.0
Commit c6da87ff4b (2019-08-20 00:03 UTC)
OS: macOS (x86_64-apple-darwin18.6.0)
CPU: Intel® Core™ i7-7920HQ CPU @ 3.10GHz
LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
JULIA_NUM_THREADS = 4
JULIA_EDITOR = atom -a
PS: using threads speeds it up (~2x), but that’s as far as I get