# Is there a line profiler

**URL:** <https://discourse.julialang.org/t/is-there-a-line-profiler/24227>\
**Category:** New to Julia\
**Created:** [May 15, 2019, 6:49am UTC](https://discourse.julialang.org/t/is-there-a-line-profiler/24227 "2019-05-15T06:49:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [May 15, 2019, 6:49am UTC](https://discourse.julialang.org/t/is-there-a-line-profiler/24227/1 "2019-05-15T06:49:56Z")

</div>

I have rewritten some python code in julia. Now I see that the Julia code is 1/3 faster than the naive python code, but still a factor of two slower than the optimized one.

Is there a line profiler for Julia which can check where I spend the most time in the Julia code?

I suspect the following function to be called most, but I don’t know.

```julia
function fox_goodwin_step!(w_p1,w_0,w_m1,r)
        aa = 2*I+10*w_0
        bb = (I-w_m1)*r
        cc = I-w_p1
        r .= (aa.-bb)\cc
        w_m1 .= w_0
        w_0 .= w_p1
        r
end

```

The project is located here: [https://github.com/feanor12/HASlib.jl](https://github.com/feanor12/HASlib.jl)

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [May 15, 2019, 7:46am UTC](https://discourse.julialang.org/t/is-there-a-line-profiler/24227/2 "2019-05-15T07:46:20Z")

</div>

Did you try [`Profile`](https://docs.julialang.org/en/v1/manual/profile/index.html)? That should be able to confirm your suspicion about where most of the time is going.

What optimization tricks does the python code play, presumably you can just do the same thing in julia? Are the matrices large? If so you’ll probably get similar performance.

By the way you can link directly to the source by clicking on a line in the github source viewer:

> <https://github.com/feanor12/HASlib.jl/blob/8fc100166de2dc5f77e29f1f2d7de11b3b919608/src/close_coupling.jl#L24>

One thing I can see here is that you’re not reusing the storage for the temporary arrays `aa`,`bb`,`cc`. That might be important. What size are these matrices?

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [May 15, 2019, 9:49am UTC](https://discourse.julialang.org/t/is-there-a-line-profiler/24227/3 "2019-05-15T09:49:24Z")

</div>

Also try ProfileView!

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [May 15, 2019, 10:40am UTC](https://discourse.julialang.org/t/is-there-a-line-profiler/24227/4 "2019-05-15T10:40:51Z")

</div>

I tried profile but in the trace the maximum number i found was 5. Do I have to increase the amount of measurements in this case? The runtime of one evaluation is around 300ms.  
In the optimized python code I work a lot with vectorization using numpy as well as cython to avoid overhead on the inner loop. The matrices can be quite small 20x20, but in some cases can also be around 200x200. So not really big, I guess.  
I’ll try to allocate aa,bb,cc outside the loop. Thanks for the hint.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [May 15, 2019, 11:25am UTC](https://discourse.julialang.org/t/is-there-a-line-profiler/24227/5 "2019-05-15T11:25:56Z")

</div>

You must be on Windows, where the interval between samples is larger. (On Linux you would have gotten ~300 samples.) Yes, try running it multiple times.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [May 15, 2019, 11:43am UTC](https://discourse.julialang.org/t/is-there-a-line-profiler/24227/6 "2019-05-15T11:43:04Z")

</div>

You can check how many allocations `fox_goodwin_step!` does using `@time fox_goodwin_step!(...)` (just keep in mind the first run will be contaminated by JIT compilation overhead, both in time and allocations).

If you make it allocation free that will help, but by the looks will uglify the implementation. You’ll need:

- some named working arrays (aa,bb,cc at least)
- more in place broadcasting with `.=`
- `LinearAlgebra.mul!` for in place matrix multiplication
- Probably `lu!` plus `ldiv!` to replace the `\`
- Maybe some manual loops for adding to the diagonals in place, I couldn’t see how to do this with stdlib LinearAlgebra
