One specific line of a function is taking too long
4 次查看(过去 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!