用Matlab循环中的变量做某事比什么都不做更快 [英] Doing something is faster than doing nothing with a variable in Matlab loop

查看:148
本文介绍了用Matlab循环中的变量做某事比什么都不做更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在分析一个微不足道的循环可能有多快的过程中,我遇到了这个奇怪的现象.

In the process of analyzing how fast a trivial loop could be, I encountered this strange phenomenon.

对变量不执行操作比对变量执行操作要慢得多.

Doing nothing with a variable is much slower than doing something with it.

当然这不是一个真正的问题,因为您不会经常感到编写无所事事的代码的冲动,但这令我感到惊讶,所以我想知道是否有人了解发生了什么以及在实际情况下这是否可能是一个问题

Of course this is not a real problem as you won't often feel the urge to write code that does nothing, but this surprised me so I wonder if anyone understands what is happening and whether this could be a problem in real situations.

这是我发现的东西:

     tic,for t= 1:1e6, x=x; end,toc %This runs very fast, about 0.07 sec
y=x; tic,for t= 1:1e6, y=x; end,toc %This runs fast, about 0.11 sec
     tic,for t= 1:1e6, x; end,toc   %This takes over half a second?!

我尝试在循环中添加一个简单的计算,以确保不会对循环进行优化,但这不会改变结果.

I tried adding a trivial calculation in the loop to ensure the loop would not be optimized away but this did not change results.

总而言之,我的问题是:

To summarize, my question is:

发生了什么事,我应该担心吗?

推荐答案

我相信您在脚本或命令行中运行了代码.如果在一个函数中运行它,您将看到所有3个变体花费几乎完全相同的时间.在命令行中,Matlab无法利用其所有可用的优化功能,因此前两个变体已得到优化,而第三个变体未进行优化.这不是很在意,因为Matlab代码通常在功能内运行,这些功能可以使优化的有效性最大化.

I believe that you ran your code within a script or in the command line. If you run it within a function you will see that all 3 variants take almost exactly the same amount of time. In the command line, Matlab cannot employ all of its available optimizations and so the first 2 variants are optimized while the third is not. This is not very concerning, since Matlab code is typically run within functions, where the optimizations' effectiveness is maximized.

在比较不同的执行路径以提高性能时,应该始终在一个函数内进行测试,就像在命令提示符下进行测试一样容易且诱人.

When comparing different execution paths for performance, you should always test within a function, as tempting and easy as it is to test in the command prompt.

p.s. -在这种特殊情况下,不是JIT出错,而是解释器的另一组(非JIT)加速问题.相对于命令行和功能的行为与JIT相同,即功能内可用的许多优化在CL中不可用.您可以看到feature jit off在时序可变性上没有任何实际差异(它使变体#1 +#2变慢,但仍比变体#3快),从而可以看到这一点-但是当您运行feature accel off时,所有差异消除了变体之间的差异,它们都以变体#3的慢速运行(CL中为0.5秒,功能中为0.25秒). JIT显然是accel的子集,因此关闭accel也会关闭JIT(反之亦然).

p.s. - In this particular case, it is not the JIT that is at fault but rather the interpreter's other (non-JIT) set of accelerations. The behavior vis-a-vis command-line vs. function is the same as with JIT, namely that many optimizations available within functions are unavailable in the CL. You can see this by seeing that feature jit off makes no real difference regarding the timing variability (it makes variants #1+#2 slower, but they are still faster than variant #3) - but when you run feature accel off all the differences between the variants are eliminated and all of them run at the slow speed of variant #3 (0.5 secs in the CL, 0.25 secs in a function). JIT is a apparently subset of accel, so turning accel off also turns off JIT (but not vise versa).

这篇关于用Matlab循环中的变量做某事比什么都不做更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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