# Why is this code so slow in julia compared to a numpy implementation?

**URL:** <https://discourse.julialang.org/t/why-is-this-code-so-slow-in-julia-compared-to-a-numpy-implementation/6660>\
**Category:** Performance\
**Tags:** performance\
**Created:** [October 24, 2017, 4:20pm UTC](https://discourse.julialang.org/t/why-is-this-code-so-slow-in-julia-compared-to-a-numpy-implementation/6660 "2017-10-24T16:20:47Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [October 24, 2017, 4:34pm UTC](https://discourse.julialang.org/t/why-is-this-code-so-slow-in-julia-compared-to-a-numpy-implementation/6660/4 "2017-10-24T16:34:53Z")

</div>

The first thing is to read through the [Performance Tips](https://docs.julialang.org/en/stable/manual/performance-tips/#) section of the manual.

The main general suggestions are

1. never to benchmark in global scope (because then all your variables are global variables and can change at any time, so the compiler can’t generate optimized code)
2. use `@code_warntype` to check for any places where Julia couldn’t infer your types statically (at compile-time), which is often the reason for slow performance.

Also, your code is much easier to read if you wrap it in back-ticks so it shows up highlighted and code-formatted, and when indented properly will look quite nice. The easier you make it on the reader the more likely you are to get more detailed feedback and help.

---

_[View the full topic](https://discourse.julialang.org/t/why-is-this-code-so-slow-in-julia-compared-to-a-numpy-implementation/6660)._
