Unexplainable huge performance degradation

2 Ansichten (letzte 30 Tage)
Javier Ros
Javier Ros am 1 Okt. 2018
Kommentiert: Stephen23 am 2 Okt. 2018

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 Kommentare
Dennis
Dennis am 2 Okt. 2018
You should show the code of your scripts. Using a script with some simple math i can not reproduce your results.
tic
for i=1:10000
script_1
end
toc
tic
for i=1:10000
script_2
end
toc
Elapsed time is 0.203584 seconds.
Elapsed time is 0.274638 seconds.
Stephen23
Stephen23 am 2 Okt. 2018
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.
>>

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Stephen23
Stephen23 am 2 Okt. 2018
Bearbeitet: Stephen23 am 2 Okt. 2018
"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 Kommentare
Javier Ros
Javier Ros am 2 Okt. 2018
Bearbeitet: Javier Ros am 2 Okt. 2018
Thank you all for your answers.
I have to apologize, because there was a call in script_2 that I have not seen because it was at the end of the file separated by a bunch of white lines. That was the real performance spoiler.
Anyway, I've tried your suggestions and converting script_1 to a function has a positive effect. Paradoxically removing globals from the function and passing these variables as arguments to the function has shown a performance loss (2-3 times slower).
Than you all for your suggestions, they have all been very valuable!.
Javier
Stephen23
Stephen23 am 2 Okt. 2018
"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.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by