在一个单元阵列存储处理对象时性能下降 [英] Slow performance when storing handle objects in a cell array

查看:155
本文介绍了在一个单元阵列存储处理对象时性能下降的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量的性能我的 MATLAB code的只是一个很小的部分问题的希望,你可能有一个想法,如何改进它:

我开发在MATLAB基于代理的模拟,创建大量手柄对象。其中有些是代理别人可以例如由代理所拥有的对象。

要清楚地确定每个处理对象的每一个得到一个通过 IdDistributor 的对象发出一个唯一的ID (obj.Id)。在 IdDistributor 的本身交给每个对象的构造函数是resive一个ID,然后从那里叫给了一个ID号( giveId 的)。

在另外的 IdDistributor 的保持一种电话簿(一个的 IdRegistry 的),每个标识与对象相关联。所以给出的标识可以查找对象在 IdRegistry

我通过使用存储在正是领域,他们的标识匹配不同的处理对象的单元阵列来实现这一点。 (普通阵不起作用,因为对象是不同类的)。

测试我的模拟实在是慢,MATLAB的概述的显示,99%的时间都花在与 IdDistributor 的特别是与存储在对象的行的 IdRegistry 的(用了有点像每个物体1秒的时候,我试图创造约10,000个对象)。

现在我试图来个类似的解决方案,花费较少的时间即可。正如你可以在code见下文我已经尝试来提高速度与pre-分配(我延长的 IdRegistry 的由10000个细胞时,它是满的,而不是估量1 1)。我也想过试图以某种方式获取处理对象的MATLAB内部ID,但没有按照这条道路,当我读到与该ID是不是永久性的,可以由系统进行修改。

我真的太多AP preciate任何想法或者如何加快code或找到一种解决方法/改善我的概念!

在这里我的code:

最慢行的 IdDist.IdRegistry(IDNumber中)= {} OBJ;

BTW。将其更改为的 {IdDist.IdRegistry}的ID号的obj =; 的没有多大帮助。

  classdef IdDistributor<处理性能
    ID = int64类型(1);她%自己的ID
    LastId = Int64的(1);
    IdRegistry = {}
结束方法
    功能IdDist = IdDistributor()
        IdDist.Id = Int64的(1);
        IdDist.LastId = Int64的(1);
        IdDist.register(IdDist);
    结束
    功能IDNUM = giveId(IdDist,OBJ)
        IDNUM = IdDist.LastId + 64位整数(1);
        IdDist.LastId = IDNUM;
        IdDist.register(OBJ,IDNUM)
    结束
    功能寄存器(IdDist,OBJ,IDNUM)
        如果nargin == 2
            的ID号= obj.Id;
        ELSEIF nargin == 3
            的ID号= IDNUM;
        结束
            如果IDNumber中> =长度(IdDist.IdRegistry)%10000延长注册
              IdDist.IdRegistry(IDNumber中+ 10000)= {[]};
            结束
            如果IDNumber中大于0
              IdDist.IdRegistry(IDNumber中)= {} OBJ;
            结束
    结束%功能
    结束%方法
    结束%类


解决方案

既然你不删除从注册表对象,你可能想尝试从的 matlab.mixin.Heterogeneous 类,然后将它们存储作为​​一个普通的数组。请注意,这将需要R2011a或更高版本。

我不知道这是速度更快,但它的一些尝试。当然,这只会是有用的,如果所有的ID由IdDistributor产生的,因为他们是有序的。

另外,我的测试表明,

 长度(IdDist.IdRegistry)

也慢,所以你可以存储在注册表中的 IdDistributor 以及长度。我建议设置 SetAccess 保护的性能,以保证安全。

I have massive performance problems with just a very little portion of my MATLAB Code an hope you might have an idea how to improve it:

I am developing an agent based simulation in MATLAB that creates lots of handle objects. Some of them are agents others can be e.g. objects that are owned by the agents.

To clearly identify each of these handle object every single one gets a unique Id (obj.Id) that is issued by an "IdDistributor" object. The IdDistributor itself is handed over to the constructor of each object that is to resive an Id and is called from there to give out an Id-number (giveId).

In addition the IdDistributor keeps a sort of phone book (an IdRegistry) that associates each Id with the object. So given the Id one can look up the object in the IdRegistry.

I implemented this by using a cell array that stores the different handle objects in exactly that field that matches their Id. (Normal array does not work since the objects are of different classes).

Testing my simulation it is really slow and the MATLAB Profiler shows that 99% of the time is spent with the IdDistributor especially with the line that stores the objects in the IdRegistry (It took something like 1 seconds per object when I tried to create about 10,000 objects).

Now I'm trying to come to a similar solution that takes less time. As you can see in the code below I have already tried to increase Speed with pre-allocation (I extend the IdRegistry by 10,000 cells when it is full, instead of sizing up 1 by 1).I also thought about trying to somehow get the MATLAB internal Id of the handle objects but didn't follow that road when I read that that Id is not permanent and can be changed by the system.

I would really much appreciate any ideas either how to speed up the code or to find a workaround/improvement to my concept!

Here my Code:

The slowest line is IdDist.IdRegistry(IdNumber)={obj};

btw. changing it to IdDist.IdRegistry{IdNumber}=obj; didn't help much

classdef IdDistributor < handle

properties
    Id=int64(1); %Her own ID
    LastId=int64(1);
    IdRegistry={}
end

methods
    function IdDist=IdDistributor()
        IdDist.Id=int64(1);
        IdDist.LastId=int64(1);
        IdDist.register(IdDist);
    end
    function IdNum=giveId(IdDist,obj)
        IdNum=IdDist.LastId+int64(1);
        IdDist.LastId=IdNum;
        IdDist.register(obj,IdNum)
    end
    function register(IdDist,obj,IdNum)
        if nargin==2      
            IdNumber=obj.Id;
        elseif nargin==3
            IdNumber=IdNum;
        end
            if IdNumber>=length(IdDist.IdRegistry) %Extend the Register by 10000
              IdDist.IdRegistry(IdNumber+10000)={[]};    
            end
            if IdNumber >0
              IdDist.IdRegistry(IdNumber)={obj};
            end
    end %function
    end %methods
    end %class

解决方案

Since you're not deleting objects from your registry, you might like to try deriving all your objects from the matlab.mixin.Heterogeneous class, then storing them as a regular array. Note that this will require R2011a or newer.

I don't know if this is faster, but it's something to try. Of course it'll only be useful if all your IDs are generated by the IdDistributor, since they're sequential.

Also, my tests suggest that

length(IdDist.IdRegistry)

is also slow, so you could store the length of the registry in the IdDistributor as well. and I would suggest setting the SetAccess to protected for the properties, for safety.

这篇关于在一个单元阵列存储处理对象时性能下降的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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