One specific line of a function is taking too long

4 views (last 30 days)
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 Comments
per isakson
per isakson on 1 Mar 2014
Edited: per isakson on 1 Mar 2014
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.)
Alessandro
Alessandro on 1 Mar 2014
Edited: Alessandro on 1 Mar 2014
That's what I am trying to find out. Apparently the problem is the fact that an input variable changes during the call. I will investigate this tomorrow. Anyways I'm using 2013a as well.

Sign in to comment.

Answers (2)

per isakson
per isakson on 1 Mar 2014
Edited: per isakson on 4 Mar 2014
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 Comments
per isakson
per isakson on 2 Mar 2014
Edited: per isakson on 2 Mar 2014
  • 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
Alessandro on 2 Mar 2014
This last result is obtained by the same model I had before. I can't access my PC on weekends, I just have it run and sendmail when it finishes running the script. Anyways that runtime is outrageous. Hope they are going to give me a good explanation for that!

Sign in to comment.


Alessandro
Alessandro on 3 Mar 2014
New informations about the issue:
  • Making K global doesn't change anything. Still same runtime.
  • By-passing the function and pasting those 4 lines inside the main code, of course, solves the issue, but I honestly do not like to do that. Especially since I do not understand the reason why the error shows up.
This is the new runtime for those particular 4 lines of code (same as the "Version 1" in my original post):
0.02 compared to the previous 29 seconds is a 145000% difference. :D
  4 Comments
per isakson
per isakson on 4 Mar 2014
Edited: per isakson on 4 Mar 2014
  • 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.
Alessandro
Alessandro on 5 Mar 2014
In my script, passing K to the function as global, did not change runtime. Still have no clue about this. Although your experiment proves it may speed things up. Also, it is very interesting that you noticed the long runtime disappears while using sub-functions!

Sign in to comment.

Categories

Find more on Multicore Processor Targets in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!