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

The first thing is to read through the 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.

3 Likes