MATLAB在函数的最后一行之后需要很长时间 [英] MATLAB takes a long time after last line of a function

查看:196
本文介绍了MATLAB在函数的最后一行之后需要很长时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要很长时间才能运行的功能。当我剖析它时,我发现超过一半时间(50秒中有26秒)没有在逐行时间细分中考虑,并且我可以显示时间是在函数完成运行之后但在它返回控制之前花费的时间通过以下方法:

  ts1 = tic; 
disp('calling function');
functionCall(args);
disp(['control returned to caller - ',num2str(toc(ts1))]);

函数I的第一行是 ts2 = tic ,最后一行是

  disp(['function-'的最后一行,num2str(toc(ts2 ))]); 

结果为

lockquote

调用函数

函数的最后一行 - 24.0043

控制返回给调用者 - 49.857

p>

在互联网上探索,我认为这是MATLAB管理内存方式的一个症状。它在函数返回时释放,有时需要很长时间。该函数确实分配了一些大的(〜100万个元素)数组。它也适用于句柄,但不会明确地创建任何新的句柄对象或存储句柄。我的问题是:


  1. 这绝对是内存管理问题吗?

  2. 有没有系统的方法来诊断这个函数中导​​致问题的原因是什么,而不是其他的快速返回?

  3. 是否有减少MATLAB花费在函数出口上的时间量的一般技巧?
  4. li>


解决方案

我发现了一个可能适用于我的特定问题的修复方法。



需要很长时间退出的函数在包含句柄对象向量的基本对象上调用。当我改变基本对象的定义来扩展句柄时,我消除了函数结束时的滞后。



我相信发生的事情是:当我通过对我的函数的基本对象,它创建了该对象的副本(默认情况下,MATLAB通过值传递)。这并不需要太多时间,但是当函数退出时,它会销毁对象副本,从而导致它查看句柄对象的矢量,以确保没有需要清理的孤儿。我相信正是这个操作让MATLAB花了很长时间。



当我改变传递给句柄的对象时,函数工作空间中没有进行复制,所以最后不需要清理对象。



这对我来说是一条通用规则:

如果函数花费很长时间在退出时清理它的工作空间,并且按值传递大量数据或复杂结构,尝试将该函数的参数封装在句柄对象中

这将避免重复,并因此在退出时进行耗时的清理。缺点是你的函数现在可能会意外地改变你的输入,因为MATLAB不能像c ++中那样声明一个参数const。


I have a function that's taking a long time to run. When I profile it, I find that over half the time (26 out of 50 seconds) is not accounted for in the line by line timing breakdown, and I can show that the time is spent after the function finishes running but before it returns control by the following method:

ts1 = tic;
disp ('calling function');
functionCall(args);
disp (['control returned to caller - ', num2str(toc(ts1))]); 

The first line of the function I call is ts2 = tic, and the last line is

disp (['last line of function- ', num2str(toc(ts2))]);

The result is

calling function

last line of function - 24.0043

control returned to caller - 49.857

Poking around on the interwebs, I think this is a symptom of the way MATLAB manages memory. It deallocates on function returns, and sometimes this takes a long time. The function does allocate some large (~1 million element) arrays. It also works with handles, but does not create any new handle objects or store handles explicitly. My questions are:

  1. Is this definitely a memory management problem?
  2. Is there any systematic way to diagnose what causes a problem in this function, as opposed to others which return quickly?
  3. Are there general tips for reducing the amount of time MATLAB spends cleaning up on a function exit?

解决方案

I discovered a fix to my specific problem that may be applicable in general.

The function that was taking a long time to exit was called on a basic object that contained a vector of handle objects. When I changed the definition of the basic object to extend handle, I eliminated the lag on the close of the function.

What I believe was happening is this: When I passed the basic object to my function, it created a copy of that object (MATLAB is pass by value by default). This doesn't take a lot of time, but when the function exited, it destroyed the object copy, which caused it to look through the vector of handle objects to make sure there weren't any orphans that needed to be cleaned up. I believe it is this operation that was taking MATLAB a long time.

When I changed the object I was passing to a handle, no copy was made in the function workspace, so no cleanup of the object was required at the end.

This suggests a general rule to me:

If a function is taking a long time to clean up its workspace on exiting and you are passing a lot of data or complex structures by value, try encapsulating the arguments to that function in a handle object

This will avoid duplication and hence time consuming cleanup on exit. The downside is that your function can now unexpectedly change your inputs, because MATLAB doesn't have the ability to declare an argument const, as in c++.

这篇关于MATLAB在函数的最后一行之后需要很长时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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