MATLAB函数第一次变慢,但随后变快得多.为什么? [英] MATLAB function is slow the first time, but much faster subsequently. Why?

查看:540
本文介绍了MATLAB函数第一次变慢,但随后变快得多.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的MATLAB函数文件.它首先创建一个零矩阵,然后通过评估在函数中进行硬编码的多个相应的(长)代数表达式来更新约70%的单元.完成此操作后,将返回一个数值矩阵.

I have a large MATLAB function file. It first creates a zero matrix, then updates about 70% of the cells by evaluating a number of corresponding (long) algebraic expressions that are hard coded in the function. Once this is complete, a numeric matrix is returned.

.m文件的大小约为4 MB(我有100个此类m.文件,但这并不直接相关).当我第一次评估该函数时,大约需要9秒钟的时间来评估.但是,随后的运行只需要大约0.1秒钟,这比我期望的要多.

The .m file is about 4 MB large (I have 100 of these m. files, but that is not directly relevant). When I evaluate the function the first time, it takes about 9 seconds to evaluate. Subsequent runs, however, only take about 0.1 second, which is more what I was expecting.

为什么第一次评估要花9秒钟?每当我关闭并重新打开MATLAB时,我每次都会进行这种缓慢的首次评估,而随后的运行会更快.为什么会这样?

Why does the first evaluation take 9 seconds? Anytime I close and reopen MATLAB, I each time have this slow first evaluation, with subsequent runs being much faster. Why is this?

米.可以在下面的公共链接中找到文件(您可以从浏览器复制文本): https://dl.dropboxusercontent.com/u/157153767/K_a_12_102x.m

The m. file can be found at the below public link (you can copy the text from the browser): https://dl.dropboxusercontent.com/u/157153767/K_a_12_102x.m

您应该使用的命令窗口输入是: [测试] = K_a_12_102x(414000000,1.1095e + 09,1.2500e-04,0.0840,0.0840,0.0240,0.0240,0.0020,0.0020,0,0,0,0,3.0397e + 08,8.9930e + 07,0, 3.0397e + 08,0,1.0702e + 08,0,0,0,0,0,0,497.7389,80.7355,-15.9811,391.1985,-15.9811,103.5248,20440000,0,20440000,0.06)

The command window input you should use is: [test]=K_a_12_102x(414000000,1.1095e+09,1.2500e-04,0.0840,0.0840,0.0240,0.0240,0.0020,0.0020,0,0,0,0,3.0397e+08,8.9930e+07,0,3.0397e+08,0,1.0702e+08,0,0,0,0,0,0,497.7389,80.7355,-15.9811,391.1985,-15.9811,103.5248,20440000,0,20440000,0.06)

推荐答案

我认为这是JIT编译.第一次执行文件时,MATLAB必须对其进行解释(将文本转换为机器代码).

I think that's the JIT-compilation. The first time you execute the file, MATLAB has to interpret it (translating text to machine code).

后续运行将使用缓存的机器代码,并且执行速度更快.要验证这一点:在代码中进行了少量更改之后,MATLAB将需要重做此编译-因此对于下一次运行它应该很慢. (我这样做的结果就是这样.)您正在做大量的简单操作,应该很快地执行.转换为机器代码会拖慢您的速度.

Subsequent runs use the cached machine code and execute much faster. To verify this: After a small change in the code, MATLAB will need to redo this compilation - so it should be slow for the next run again. (I did this with exactly that consequence.) You are doing a significant amount of simple operations, that should execute pretty quick. The conversion to machine code is slowing you down.

要加速整个过程:将代码传输到C,C#或类似的文件中,并将其作为DLL文件包含在内.您将获得恒定且快速的计算,但是无法轻松地对其进行更改.

To speed the whole thing up: Transfer the code to C, C# or something similar and include it as a DLL file. You will have constant and fast calculations, but you cannot change them as easily.

(使用C#DLL文件,您还可以进行一些JIT编译,但是它比MATLAB的访问频率更低,而且可能仍然比MATLAB更快.)

(Using a C# DLL file, you've got some JIT compilation as well, but it's less often and probably still faster than MATLAB.)

我已经完成了一些编码,并将代码移植到了C#中.原始时间为13.4秒和0.15秒(第一次/第二次运行)

I've done some coding and ported the code to C#. Original timings are 13.4 s and 0.15 s (first/second run)

简单端口,发布配置:

>> test_1
Elapsed time is 124.7 seconds.
>> test_1
Elapsed time is 0.0297 seconds.

因此,第一次运行比MATLAB差很多.虽然我很喜欢C#,但它可能并不是这项工作的最佳工具. (顺便说一句:我不得不使用命令行编译器,因为Visual Studio 2010一直崩溃...)

So the first run is much worse than MATLAB - bummer. While I love C# as it is, it might not be the best tool for this job. (BTW: I had to use the command-line compiler because Visual Studio 2010 kept crashing...)

这篇关于MATLAB函数第一次变慢,但随后变快得多.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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