# Optimization tips for my julia code. Can I make it even faster and/or memory efficient?

**URL:** <https://discourse.julialang.org/t/optimization-tips-for-my-julia-code-can-i-make-it-even-faster-and-or-memory-efficient/34614>\
**Category:** Performance\
**Tags:** question, python\
**Created:** [February 14, 2020, 8:54am UTC](https://discourse.julialang.org/t/optimization-tips-for-my-julia-code-can-i-make-it-even-faster-and-or-memory-efficient/34614 "2020-02-14T08:54:26Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [February 14, 2020, 2:58pm UTC](https://discourse.julialang.org/t/optimization-tips-for-my-julia-code-can-i-make-it-even-faster-and-or-memory-efficient/34614/21 "2020-02-14T14:58:37Z")

</div>

Ha, never trust a random guy from internet. Yes, you are right, `centroids_cnt` should be moved to the outer loop. With this change results less amazing kind of ~9.5s which is still good.

I’ve updated gist: [https://discourse.julialang.org/t/optimization-tips-for-my-julia-code-can-i-make-it-even-faster-and-or-memory-efficient/34614/8 · GitHub](https://gist.github.com/Arkoniak/f8abdc8dc89e4a3b501c781eac4e739d)

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [February 14, 2020, 8:26pm UTC](https://discourse.julialang.org/t/optimization-tips-for-my-julia-code-can-i-make-it-even-faster-and-or-memory-efficient/34614/22 "2020-02-14T20:26:12Z")

</div>

That’s the last one 🙂  
After implementing custom `pairwise` function, refactoring `sum_of_squares`, and reordering loops in order to respect memory layout, I was finally able to achieve ~5s of execution time (now for real). It’s in the same gist, so I wouldn’t repeat link here. Which is even more interesting, new version can beat `Clustering` implementation.

```julia
using Clustering
@btime kmeans(transpose($X), 2) # 187.700 ms (95 allocations: 83.93 MiB)
@btime Kmeans($X, 2, verbose = false, tol=1e-10) # 118.517 ms (27 allocations: 45.78 MiB)

```

The next interesting step is the parallelization of this problem, it should be easy enough. Even more interesting would be to try to parallelize it on GPU, something tells me that it is going to be spectacular.

---

<div class="post-metadata">

**Author:** ![PyDataBlog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pydatablog/32/12327_2.png) [@PyDataBlog](https://discourse.julialang.org/u/PyDataBlog)\
**Post date:** [February 14, 2020, 9:17pm UTC](https://discourse.julialang.org/t/optimization-tips-for-my-julia-code-can-i-make-it-even-faster-and-or-memory-efficient/34614/23 "2020-02-14T21:17:47Z")

</div>

Maybe I should package it and serve it a Julia package?

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [February 15, 2020, 1:27pm UTC](https://discourse.julialang.org/t/optimization-tips-for-my-julia-code-can-i-make-it-even-faster-and-or-memory-efficient/34614/24 "2020-02-15T13:27:35Z")

</div>

Clustering.jl is an existing and popular package for doing clustering.  
I suggest benchmarking against it’s current implementation, and if your code is faster, make a PR.

---

<div class="post-metadata">

**Author:** ![PyDataBlog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pydatablog/32/12327_2.png) [@PyDataBlog](https://discourse.julialang.org/u/PyDataBlog)\
**Post date:** [February 15, 2020, 2:22pm UTC](https://discourse.julialang.org/t/optimization-tips-for-my-julia-code-can-i-make-it-even-faster-and-or-memory-efficient/34614/25 "2020-02-15T14:22:49Z")

</div>

> [@oxinabox](#):
>
> I suggest benchmarking against it’s current implementation, and if your code is faster, make a PR.

@Skoffer would be the best person for the PR since he came up with the optimizations. From all the benchmarking results, this implementation is faster and more memory efficient than the current implementation in Clustering.jl.

It’s worth making a PR @Skoffer 🙂 .

[Previous page](https://discourse.julialang.org/t/optimization-tips-for-my-julia-code-can-i-make-it-even-faster-and-or-memory-efficient/34614.md?page=1)
