# How do I speed this up?

**URL:** <https://discourse.julialang.org/t/how-do-i-speed-this-up/39577>\
**Category:** Performance\
**Created:** [May 16, 2020, 10:12am UTC](https://discourse.julialang.org/t/how-do-i-speed-this-up/39577 "2020-05-16T10:12:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![scotts](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@scotts](https://discourse.julialang.org/u/scotts)\
**Post date:** [May 16, 2020, 10:12am UTC](https://discourse.julialang.org/t/how-do-i-speed-this-up/39577/1 "2020-05-16T10:12:37Z")

</div>

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

The culprit loop is:

```julia
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???

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [May 16, 2020, 10:25am UTC](https://discourse.julialang.org/t/how-do-i-speed-this-up/39577/2 "2020-05-16T10:25:12Z")

</div>

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.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [May 16, 2020, 10:36am UTC](https://discourse.julialang.org/t/how-do-i-speed-this-up/39577/3 "2020-05-16T10:36:26Z")

</div>

> [@scotts](#):
>
> fill(Int(0),((n\_ages-1), n\_cols))

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!.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 16, 2020, 1:55pm UTC](https://discourse.julialang.org/t/how-do-i-speed-this-up/39577/4 "2020-05-16T13:55:33Z")

</div>

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.

> [@scotts](#):
>
> ```julia
> for i::Int16 = 1 : n_acts 
> n_ages::Int16 = m["max_age"][i]
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![scotts](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@scotts](https://discourse.julialang.org/u/scotts)\
**Post date:** [May 17, 2020, 5:17am UTC](https://discourse.julialang.org/t/how-do-i-speed-this-up/39577/5 "2020-05-17T05:17:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [May 17, 2020, 5:57am UTC](https://discourse.julialang.org/t/how-do-i-speed-this-up/39577/6 "2020-05-17T05:57:12Z")

</div>

A quick glance through the code suggests some other places for easy performance gains, but as a first starting point, the [general performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/) are a good ressource.

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [May 17, 2020, 11:22am UTC](https://discourse.julialang.org/t/how-do-i-speed-this-up/39577/8 "2020-05-17T11:22:57Z")

</div>

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