# Making efficient some small subroutines

**URL:** https://discourse.julialang.org/t/making-efficient-some-small-subroutines/38917
**Category:** Performance
**Created:** [May 6, 2020, 10:41pm UTC](https://discourse.julialang.org/t/making-efficient-some-small-subroutines/38917 "2020-05-06T22:41:31Z")
**Posts on this page:** 3
**Page:** 2

<div class="post-metadata">

### Author: ![eliassno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eliassno/32/18917_2.png) [@eliassno](https://discourse.julialang.org/u/eliassno)
#### Post date: [May 8, 2020, 12:34am UTC](https://discourse.julialang.org/t/making-efficient-some-small-subroutines/38917/21 "2020-05-08T00:34:23Z")

</div>

After fixing the line `D[i,p] = ldiv!(lu!(B), Dl)` and changing your program to use in-place functions, I’d think further optimizations would be microscopic.

> [@O\_Julia](#):
>
> ```julia
> ...
> @inbounds for j in 1:n 
> a = (X[i]-X[p[j]]); b=(Y[i]-Y[p[j]]); c=sqrt(a^2 + b^2); 
> h1[j] = D(c, a, 2,γ,sh) 
> h2[j] = D(c, b, 2,γ,sh) 
> end
> ...
> 
> ```

I don’t know if you’ll get any speedup, but in the function `H(i,n,X,Y,p,γ,sh)`, you could move the square root in the assignment of `c` to the exponents within `D(c, _, 2,γ,sh)`.

---

<div class="post-metadata">

### Author: ![eliassno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eliassno/32/18917_2.png) [@eliassno](https://discourse.julialang.org/u/eliassno)
#### Post date: [May 8, 2020, 12:36am UTC](https://discourse.julialang.org/t/making-efficient-some-small-subroutines/38917/22 "2020-05-08T00:36:14Z")

</div>

Once you’re satisfied with the performance, it would be interesting to see some kind of comparison with the python version of your program.

---

<div class="post-metadata">

### Author: ![O\_Julia](https://avatars.discourse-cdn.com/v4/letter/o/3ab097/32.png) [@O\_Julia](https://discourse.julialang.org/u/O_Julia)
#### Post date: [May 8, 2020, 10:10am UTC](https://discourse.julialang.org/t/making-efficient-some-small-subroutines/38917/23 "2020-05-08T10:10:31Z")

</div>

In original full version of my program the timings were as follows:

```julia
Julia->34 seconds
Python+Numpy->28 seconds

```

After improvements in Julia codes, I get

```julia
BenchmarkTools.Trial: 
  memory estimate: 6.57 GiB
  allocs estimate: 5684639
  --------------
  minimum time: 4.599 s (2.48% GC)
  median time: 4.636 s (3.40% GC)
  mean time: 4.636 s (3.40% GC)
  maximum time: 4.673 s (4.30% GC)
  --------------
  samples: 2
  evals/sample: 1

```

This is much better. Thanks to everyone who contributed once again.

[Previous page](https://discourse.julialang.org/t/making-efficient-some-small-subroutines/38917.md?page=1)
