Need for help for performance improvement or refactoring methods on this code
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I've tried calculating some data result, but this below code is not clear and difficult to understand.
I tried to write the code cleanly and tried to use the table type, but the speed was very slow.
Is there a way to increase the readability of that code while maintaining speed? (like using nested function ... etc)
please help me.
0 comentarios
Respuestas (1)
Les Beckham
el 21 de Dic. de 2023
Wow! That code gets weird really fast.
First, you haven't posted code that will actually run. You've shown three functions but nothing about how you call them.
Starting with the first function calculation(), you aren't using several of the parameters that are passed in to this function. It looks like what is called ParameterA in the function argument list (in the line that defines the function) is being referred to within the function as MyParameter.A, for example.
Now here is where you are really going nuts.
You define what looks like a placeholder for a simple struct (Result) that you plan to fill in later with, and then on the very next line, you make that struct 10 million times bigger by assigning the 10 millionth element of what will now be a 10 million element struct array to the original value of Result.
If I run the following three lines (plus one I added to check the sizes of the variables in the workspace) on my desktop Matlab (it times out in Answers), the result is shown below.
limitNum = 10e6;
Result = struct('Value', 0, 'Total', 0, 'Parameter', 0);
Result(limitNum) = Result; %
whos
whos
Name Size Bytes Class Attributes
Result 1x10000000 240000816 struct
limitNum 1x1 8 double
Result is now over 240 megabytes. And it doesn't contain any useful data yet.
There are a lot of other instances of variables that aren't defined and other issues that make this code completely incomprehensible. You need to explain what you are trying to do Add comments to the code to explain what it is supposed to do, this will help us to know how to help you actually make it do that. Post code that will actually run (or at least generate an error message) and any data that is needed to run it.
3 comentarios
Les Beckham
el 21 de Dic. de 2023
Does the Result struct really need to have 10 million elements?
To get useful help you are going to need to do more than add comments (though that will help).
Make sure that you post code that we can actually run (even if it generates error messages), and explain more clearly what the code is supposed to be doing. The three bare functions with no calling code or data don't help us understand your problem.
Show how you are calling the functions (from the command line?) including any and all data that you are passing to the functions.
Make sure that the arguments to your functions are actually used inside those functions, using the same names that are in the argument list (e.g., ParameterA vs. MyParameter.A as I mentioned previously).
The Matlab code Editor should have flagged a lot of these issues via its automatic code analysis. Pay attention to those messages.
I'd suggest reading through this tutorial on using Matlab Answers to help you improve your chances of getting useful help with your question:
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!