朱莉娅执行速度 [英] Julia speed of execution

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

问题描述

我正在对 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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆