Julia speed of execution

2020-04-10 03:31发布

问题:

I'm benchmarking Julia execution speed. I executed @time [i^2 for i in 1:1000] on Julia prompt, which resulted in something of the order of 20 ms. This seems strange, since my computer is modern with an i7 processor (I am using Linux Ubuntu).

Another strange thing is that when I execute the same command on a range of 1:10 the execution time is 15 ms.

There must be something trivial that I am missing here?

回答1:

Several things, see performance tips:

  1. Don't benchmark in global scope.
  2. Don't measure the first execution of something like this.
  3. Use BenchmarkTools.

Julia is a JIT-compiled language, so the first time you measure things, you're measuring compilation time. This is a small fixed overhead, so for anything that takes a substantial time, it's negligible, but for short-running code like this, it's almost all of the time. Non-constant global variables force the compiler to assume almost nothing about types, which tends to poison all of your performance. This is fine in some circumstances, but most of the time, you a) should write code so that the inputs are explicit parameters to functions, rather than implicit parameters coming from some globals, and b) shouldn't write code that uses mutable global state.



标签: julia