本文介绍了朱莉娅的执行速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对Julia执行速度进行基准测试.我在Julia提示符下执行了@time [i^2 for i in 1:1000],结果大约是20毫秒.这似乎很奇怪,因为我的计算机是配备i7处理器的现代化计算机(我使用的是Linux Ubuntu).

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).

另一个奇怪的事情是,当我在1:10的范围内执行同一命令时,执行时间为15毫秒.

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. 不要在全球范围内进行基准测试.
  2. 不要衡量像这样的东西的第一次执行情况.
  3. 使用 BenchmarkTools .

Julia是JIT编译的语言,因此,第一次测量时,就是在测量编译时间.这是一个很小的固定开销,因此对于花费大量时间的任何事情来说,它都是可以忽略的,但是对于像这样的短时间运行的代码,几乎是所有时间.非常数全局变量会强制编译器对类型几乎不承担任何责任,这往往会损害您的所有性能.在某些情况下这很好,但是在大多数情况下,您a)应该编写代码,以便输入是函数的显式参数,而不是来自某些全局变量的隐式参数,并且b)不应编写使用可变全局变量的代码.状态.

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.

这篇关于朱莉娅的执行速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 10:04