Converting script to function causing significant slowdown?

2 vues (au cours des 30 derniers jours)
Nick Heinz
Nick Heinz le 9 Mai 2018
Commenté : Nick Heinz le 21 Mai 2018
I have an inherited project (roughly 5k lines) where the previous author has set everything up as several scripts that all execute in the base workspace, example structure:
  • Main
  • - Script 2
  • - Script 3
  • -- Script 3b
etc.
The general application flow is to load in parameters from Excel (currently that is used as a 'front end'), run the calculations, then export the data (both to Excel and other formats). The entire processing time varies with the input parameters, but in general takes ~80 seconds to complete on my machine.
This application eventually needs to be compiled. I'm able to run it as a deployed application and the processing time is approximately the same, no issue there.
However, I have a need to call the compiled application directly from Excel, and pass it parameters while doing this. To accomplish this I simply converted the main.m script into a function, eg below
function main(input1, input2)
When I do this however, in every format I run the application now (deployed, called from Excel, native Matlab/ non-deployed) it takes significantly longer to run through. From ~80 seconds to 10+ minutes.
My question is, am I doing anything fundamentally wrong here just converting main.m to a function like I did? If so, is there a better way to pass parameters to a deployed application? (my ultimate goal) Or does anyone know why changing just my main.m into a function from a script would cause a huge slowdown?
Thanks in advance for any insight..
Win 10, Matlab 2013b (currently for compatibility issues with this project)
  21 commentaires
Greg
Greg le 15 Mai 2018
I tried adding some slightly less trivial filler code, and adding in a couple more of the non-double variables. And I installed R2013b as well. Still no luck reproducing the issue.
Nick: can you provide any more detail on the struct and the large cell array? I'm wondering if they are key components because of the relatively large memory size. Also, in general, would you say there is much cross-script variable access? For example, does script3 use or manipulate anything created in script2?
Nick Heinz
Nick Heinz le 21 Mai 2018
Thanks for continuing to look into this, I really appreciate it. Just getting a chance to work on this project again, the last few days have been extremely busy.
The large cell array is just used in the 'setup' / parameter loading portion of the app, and from the profiler tests I've done this portion of the app is approximately the same between the function and script configurations. Along those same lines, the structure variable is only used in the data export portion, so again shouldn't have an effect on the calculation / run speed.
Yes, the way it was coded there is an extreme amount of cross-script access. There is also a significant amount of variables that unfortunately can't be pre-allocated because their size is unknown at run time. I've tried to do what I can, but with the current program structure not a whole lot can be done.
If you still are interested in creating filler code, the griddedinterps play a significant role in iterative solver routine, and each of those lookups is highly nonlinear, but I think this lends more to the amount of iterations it takes to solve, and not to the root problem of why script mode is faster than function mode. So I have to use a very conservative iteration method, which results in lots and lots of iterations - I will frequently have ~200k iterations at some of the most nested scripts (3rd level nesting, if that makes sense).
@Philip - thanks for the suggestion, I will definitely try that first before a full restructure. I guess that makes, sense - like I mentioned before, function mode seems to run much closer to script mode speed in 2018a, but I am unfortunately unable to use this version due to 3rd party COM compatibility (32bit com object).

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Data Import from MATLAB dans Help Center et File Exchange

Produits


Version

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by