One thing I suspect is hurting you is that you're storing your parameters 
in a `Dict` object. Because the parameters are all of different types, (and 
because of the way you declare the `Dict`), they are stored in a `Dict{Any, 
Any}`, which means that Julia's type inference system is unable to discern 
at JIT-compilation time what the result of indexing into your `param` 
object will be. The result is that the emitted code must box everything 
that comes out of `param`, making everything that touches `param` allocate 
memory and incur a performance penalty. This would be consistent with your 
observation that removing type annotations worsens performance, since type 
annotations shouldn't matter when Julia's compiler can infer the types of 
objects passed around. Furthermore, since you set a number of loop bounds 
with values from `param`, I suspect that the type-uncertainty here hinders 
any optimization effort to remove bounds checks in those loops.

Try instead storing your parameters in a custom immutable type:

immutable Parameters
    D::Int
    T::Int
    I::Int
    max_b::Int
    w_max::Int
    gamma::Float64
    controls::Vector{Float64}
    num_controls::Int
    o_vec::Array{Int, 2}
end

You can then initialize a Parameters object such as `param = Parameters( 
... )` and replace all dict indexing with field access. Do the same for 
`dist` and `data`. (You may not be able to make these immutable, though, 
depending on whether or not you need to update them).

Finally, I'd wrap your `v1`, `term_condition` and `iter` declarations, as 
well as the subsequent `while` loop, in a `run()` function.

On Saturday, September 19, 2015 at 10:50:50 AM UTC-7, Adam wrote:
>
> Hi, I'm a novice to Julia but have heard promising things and wanted to 
> see if the language can help with a problem I'm working on. I have some 
> Matlab code with some user-defined functions that runs a simulation in 
> about ~1.4 seconds in Matlab (for a certain set of parameters that 
> characterize a small problem instance). I translated the code into Julia, 
> and to my surprise the Julia code runs 5x to 30x slower than the Matlab 
> code. I'll be running this code on much larger problem instances many, many 
> times (within some other loops), so performance is important here. 
>
> I created a GitHub gist that contains a stripped-down version of the Julia 
> code that gets as close to (as I can find) the culprit of the problem. The 
> gist is here: https://gist.github.com/anonymous/010bcbda091381b0de9e. A 
> quick description: 
>
>    - set_up.jl sets up parameters and other items.
>    - set_up_sim.jl sets up items particular to the simulation.
>    - simulation.jl runs the simulation.
>    - calc_net.jl, dist_o_i.jl, and update_w.jl are user-defined functions 
>    executed in the simulation. 
>
>
> On my laptop (running in Juno with Julia version 0.3.10), this code yields:
> elapsed time: 43.269609577 seconds (20297989440 bytes allocated, 38.77% gc 
> time)
> elapsed time: 38.500054653 seconds (20291872804 bytes allocated, 40.41% gc 
> time)
> elapsed time: 40.238907235 seconds (20291869252 bytes allocated, 39.44% gc 
> time)
>
> Why is so much memory used, and why is so much time spent in garbage 
> collection?
>
> I'm familiar with 
> http://docs.julialang.org/en/release-0.3/manual/performance-tips/ and 
> have tried to follow these tips to the best of my knowledge. One example of 
> what might be seen as low-hanging fruit: I tried removing the type 
> declarations from my functions, but this actually increased the run-time of 
> the code by a few seconds. Also, other permutations of the column orders 
> pertaining to D, T, and I led to slower performance. 
>
> I'm sure there are several issues at play here-- I'm just using Julia for 
> the first time. Any tips would be greatly appreciated. 
>

Reply via email to