One specific line of a function is taking too long
조회 수: 5 (최근 30일)
이전 댓글 표시
I am having issues with a particular function. Also, it is a very simple one, with very quick calculations in it. It serves to assemble the stiffness matrix in a Finite Element Analysis. All it does is: takes a 3x3 matrix out of a bigger one and adds an already-calculated 3x3 to it. I started noticing strange calculation times, so I tried different ways to compute that, but the results are always the same. I'm posting the results of the Profiler run on the 3 different versions of this particular function. The weird thing is that Matlab always takes a huge amount of time to run the 1st row of the program, while going fairly quick with the others.
Version 1:
Version 2:
Version 3:
Has anybody got a clue about what's taking it so long?
Thanks.
댓글 수: 7
per isakson
2014년 3월 1일
편집: per isakson
2014년 3월 1일
Why only the first line?
With R2013a this code
val = randn( 1e4 );
tic, val( 111,113 ) = val( 1111, 1133 ); toc
returns
Elapsed time is 0.000004 seconds.
(The Task Manager does not show allocating of memory.)
답변 (2개)
per isakson
2014년 3월 1일
편집: per isakson
2014년 3월 4일
This experiment on R2013a 64bit, Win7 and 8GB suggests
- try make K global
- a copy of val is created by cssm_passing_by_value(val) (and Task Manager confirms)
- however, it does not explain your 25s(?)
>> global val
>> val = randn( 1e4 );
>> cssm_global() ;
Elapsed time is 0.000007 seconds. <<<<<<<<<<<<<<<<<
Elapsed time is 0.000001 seconds.
>> val = cssm_passing_by_value( val ) ;
Elapsed time is 0.506452 seconds. <<<<<<<<<<<<<<<<<<
Elapsed time is 0.000003 seconds.
>>
where
function cssm_global()
global val
tic, val( 111,113 ) = val( 1111, 1133 ); toc
tic, val( 119,119 ) = val( 5111, 5133 ); toc
end
and where
function val = cssm_passing_by_value( val )
tic, val( 111,113 ) = val( 1111, 1133 ); toc
tic, val( 119,119 ) = val( 5111, 5133 ); toc
end
.
Surprising result with passing "by value"
I've made another few confusing experiments. (I don't think confusing, because of mistakes on my part, but [...].) Here is one such result:
- the long execution time disappears when I successively pass the array to sub-functions and change an element at each level - unbelievable
>> val = randn( 1e4 );
>> val = cssm_passing( val );
Elapsed time is 0.000004 seconds. <<<<<<<<<<<<<<<<
Elapsed time is 0.000001 seconds.
where ( I've renamed the old m-file se above.)
function val = cssm_passing ( val )
val(1,1) = 1;
val = sub_passing( val );
end
function val = sub_passing( val )
val(2,2) = 2;
val = sub2_passing( val );
end
function val = sub2_passing( val )
tic, val( 111,113 ) = val( 1111, 1133 ); toc
tic, val( 119,119 ) = val( 5111, 5133 ); toc
end
I've tested with these three functions in one m-file and in three, respectively. The result was the same in both cases.
댓글 수: 7
per isakson
2014년 3월 2일
편집: per isakson
2014년 3월 2일
- That's five and an half hour. Something is terribly wrong.
- Did you try feature('accel','on/off')?
- Did you try to "pass" K as global?
- Send the problem to the tech support. Do you have a license that allows that?
Alessandro
2014년 3월 3일
댓글 수: 4
per isakson
2014년 3월 4일
편집: per isakson
2014년 3월 4일
- You write "Making K global doesn't change anything. Still same runtime." That is not consistent with my result.
- I've added the result of another experiment to my answer. See above.
참고 항목
카테고리
Help Center 및 File Exchange에서 Multicore Processor Targets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!