Unexplainable huge performance degradation

조회 수: 4(최근 30일)
Javier Ros
Javier Ros 2018년 10월 1일
댓글: Stephen23 2018년 10월 2일

Code to reproduce problem

Case 1:

    tic,for i=1:100
    script_1
    end,toc
    Elapsed time is 0.035278 seconds.

Case 2:

    tic,for i=1:100
    script_2
    end,toc
    Elapsed time is 29.129280 seconds.

Where

script_2

is a script with a single line:a call to the first script

script_1

No output is written by either script.

It looks to me that something is wrong.

Thank you!.

Javier

  댓글 수: 3
Stephen23
Stephen23 2018년 10월 2일
Javier Ros's "Answer" moved here:
I'm sending new numbers, but figures do not improve.
Converting script_1 into a function
tic,for i=1:100
scrip_2 %as function
end,toc
Elapsed time is 36.340624 seconds.
And, additionally, converting script_1 into a function
tic,for i=1:100
script_2 %as function
end,toc
Elapsed time is 39.329580 seconds.
So it seems things are worsen a bit.
For reference
tic,for i=1:100
script_1 %as function
end, toc
Elapsed time is 0.018689 seconds.
The performance is in par as using a script.
For reference function has no parameters, and required variables from main are accessed using global
So it looks to me there is a problem.
Thanks very much,
Javier
PD: In addition to this the performance seems to worsen everytime y execute the function
>> tic,for i=1:100
script_2 %as function
end,toc
Elapsed time is 40.310388 seconds.
>> tic,for i=1:100
script_2 %as function
end,toc
Elapsed time is 41.755382 seconds.
>> tic,for i=1:100
script_2 %as function
end,toc
Elapsed time is 43.483393 seconds.
>> tic,for i=1:100
script_2 %as function
end,toc
Elapsed time is 44.249780 seconds.
>> tic,for i=1:100
script_2 %as function
end,toc
Elapsed time is 45.451738 seconds.
>> tic,for i=1:100
script_2 %as function
end,toc
Elapsed time is 47.094769 seconds.
>> tic,for i=1:100
script_2 %as function
end,toc
Elapsed time is 47.845050 seconds.
>> tic,for i=1:100
script_2 %as function
end,toc
Elapsed time is 49.758825 seconds.
>> tic,for i=1:100
script_2 %as function
end,toc
Elapsed time is 50.556079 seconds.
>>

댓글을 달려면 로그인하십시오.

답변(1개)

Stephen23
Stephen23 2018년 10월 2일
편집: Stephen23 2018년 10월 2일
"Unexplainable huge performance degradation"
Explanation: it is quite well documented that scripts are slow and so are global variables, so this is to be expected.
The documentation recommends to "Use functions instead of scripts. Functions are generally faster."
"required variables from main are accessed using global"
"So it looks to me there is a problem."
Yes there is, that problem is using global variables to pass data between functions. Using global variables is slow, so all your demonstration has shown is that exchanging one slow way of writing code (scripts) for another slow way of writing code (global variables) is still slow: "Finally, when a function call involves global variables, performance is even more inhibited. This is because to look for global variables, MATLAB has to expand its search space to the outside of the current workspace. Furthermore, the reason a function call involving global variables appears a lot slower than the others is that MATLAB Accelerator does not optimize such a function call"
Basically global variables are what beginners use instead of learning good code design:
Summary: functions are more efficient than scripts. The most efficient way to pass data is as input/output arguments, and this is what the MATLAB documentation recommends:
  댓글 수: 2
Stephen23
Stephen23 2018년 10월 2일
"removing globals from the function and passing these variables as arguments to the function has shown a performance loss"
Are the variables being altered within the function calls? It would be interesting to see the original code: please upload this by clicking the paperclip button.

댓글을 달려면 로그인하십시오.

범주

Find more on Startup and Shutdown in Help Center and File Exchange

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by