Unexplainable huge performance degradation
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Dennis
le 2 Oct 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
le 2 Oct 2018
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.
>>
Réponses (1)
Stephen23
le 2 Oct 2018
Modifié(e) : Stephen23
le 2 Oct 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 commentaires
Stephen23
le 2 Oct 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.
Voir également
Catégories
En savoir plus sur Performance and Memory dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!