2013-08-02 1 views
2

다음은 두 가지 속성을 가진 간단한 클래스입니다. PStruct는 구조체를 포함 할 속성입니다. 내가 (다른 개체의 속성을 사용하면 대신Matlab 구조체의 성능이 객체와 함께 사용되는 경우

Elapsed time is 0.001008 seconds. 
Elapsed time is 0.000967 seconds. 
Elapsed time is 0.000972 seconds. 
Elapsed time is 0.001206 seconds. 
Elapsed time is 0.000992 seconds. 
Elapsed time is 0.000981 seconds. 
Elapsed time is 0.000975 seconds. 
Elapsed time is 0.001072 seconds. 
Elapsed time is 0.000951 seconds. 
Elapsed time is 0.000994 seconds. 

을 :

clear all 
a = anobj(); % an object 
b = anobj(); % another object for future use 
ntrials=10; niterations=1000; 
a.PStruct(ntrials,niterations).field1=0; % 'initialize' the struct array 
for t=1:ntrials 
    tic; 
    for i=1:niterations 
     a.PStruct(t,i).field1=1; % store data 
    end 
    toc; 
end 

산출 : 여기

classdef anobj < handle 
    properties 
     PStruct 
     PNum=1; 
    end 
    methods 
     function obj = anobj() 
     end 
    end 
end 

1의 (꽤 빨리)와 객체의 구조를 작성하는 스크립트입니다 = 1) 루프 내에서 줄을 다음과 같이 변경합니다.

a.PStruct(t,i).field1=b.PNum; % store data 

Elapsed time is 0.112418 seconds. 
Elapsed time is 0.107359 seconds. 
Elapsed time is 0.118347 seconds. 
Elapsed time is 0.127111 seconds. 
Elapsed time is 0.138606 seconds. 
Elapsed time is 0.152675 seconds. 
Elapsed time is 0.162610 seconds. 
Elapsed time is 0.172921 seconds. 
Elapsed time is 0.184254 seconds. 
Elapsed time is 0.190802 seconds. 

뿐만 아니라 성능이 느린 크기 순서이지만, 각 재판 둔화 (더 일반적으로 확인) 매우 분명한 경향이있다 :

내가 얻을. 나는 그것을 얻지 않는다.

PStruct(t,i).field1=b.PNum; % store data 

내가 어떤 트렌드를 확인 성능을 얻을 :

Elapsed time is 0.007143 seconds. 
Elapsed time is 0.004208 seconds. 
Elapsed time is 0.004312 seconds. 
Elapsed time is 0.004382 seconds. 
Elapsed time is 0.004302 seconds. 
Elapsed time is 0.004545 seconds. 
Elapsed time is 0.004499 seconds. 
Elapsed time is 0.005840 seconds. 
Elapsed time is 0.004210 seconds. 
Elapsed time is 0.004177 seconds. 

내가 대신 개체 속성이 아닌 독립 초기화되지 않은 구조체의 배열을 사용하는 경우 또한, (이 라인은 루프 내에서 하나를 대체) 구조체 배열과 객체 사이에는 이상한 상호 작용이 있습니다. 아무도 무슨 일이 일어나고 있는지 그리고 어떻게 해결할 수 있는지 알고 있습니까? 감사.

답변

1

매우 이상합니다.

내가 발견 당신은 정상 속도에 다음 코드를 반환 중 하나를 수행 할 경우

c = b.PNum; 
a.PStruct(t,i).field1=c; % store data 

또는

a.PStruct(t,i).field1=int32(b.PNum); % store data 

하지만 당신은 이중 코드는 여전히 느리다 사용하는 경우

a.PStruct(t,i).field1=double(b.PNum); % store data 

동시에 '빠른'방법을 모두 사용하는 경우

c = b.PNum; 
a.PStruct(t,i).field1=c; % store data 
a.PStruct(t,i).field1=int32(b.PNum); % store data 

저속이 반환됩니다.

+0

재미 있습니다. 두 가지 명령으로 할당을 나누는 것이 나에게 도움이 될 수 있습니다. 감사. – matfan001

관련 문제