How do I speed this up?

I’ve translated some code from a Matlab script into Julia, but I’m finding it running much slower…

The culprit loop is:

for i::Int16 = 1 : n_acts
      n_ages::Int16 = m["max_age"][i]
      if  n_ages > 1
          idx = v[i] # variable indexes
          perm_give = -1*I(n_ages-1)
          perm_take = I(n_ages-1)
        # insert permissions in full matrix
          for t= 1 : (n_yr-1)
              #a_temp = zeros((n_ages-1), n_cols) #This added 5 seconds over the following line!
              a_temp = fill(Int(0),((n_ages-1), n_cols)) # temporary permission matrix
              a_temp[:, idx[t,1] : idx[t,n_ages-1]] = perm_give
              a_temp[:, idx[t+1,2] : idx[t+1,n_ages]] = perm_take
               #here comes the slow part....
              a_perm = cat(a_perm, a_temp;dims=1)  
          end # t loop
      end # if
  end # activity loop

As a_perm grows it gets very slow in Julia, whereas Matlab doesn’t seem to have any issues…

Note also the commented line creating a_temp. For some reason it was even slower using the zeros function compared with fill. This helped a bit, but it’s still way too slow… How can I speed things up???

There are surely some people here who see directly in your code possibilities for performance gains, but it would be much easier if you

Do your best to make your example self-contained (“minimal working example”, MWE ), so that it runs (or gets to the error that you want help with) as is. This means including package loading (e.g. using ThatPackage ) and any data that the code operates on. If your data is large or proprietary, generate example data if possible and include that.

7 Likes

You should initialize a_perm outside the loop, with its final dimensions, and the populate its value.

And you should also initalize a_perm and use fill!.

1 Like

Are you working in the global scope? That will make your code slow. Wrap your code in a function, and pass all required values as input arguments.

Unless there’s some specific reason for them, get rid of the Int16 annotation, it’s not doing any good.

5 Likes

Wow, worked a treat. Sped it up enormously. Thanks!

A quick glance through the code suggests some other places for easy performance gains, but as a first starting point, the general performance tips are a good ressource.

3 Likes

It would be nice if you post the code with the best modifications below the original one and compare timings.

1 Like