Need for help for performance improvement or refactoring methods on this code

1 回表示 (過去 30 日間)
octopus_love
octopus_love 2023 年 12 月 21 日
編集済み: octopus_love 2024 年 1 月 11 日
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.

回答 (1 件)

Les Beckham
Les Beckham 2023 年 12 月 21 日
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 件のコメント
octopus_love
octopus_love 2023 年 12 月 21 日
limitNum = 10e6;
Result = struct('Value', 0, 'Total', 0, 'Parameter', 0);
Result(limitNum) = Result; %
whos
Yes, I know this but I don't have any idea for preallocation of this 'Result' struct.
I'll add comments to the code and upload again.
Could you help me again then? thank you.
Les Beckham
Les Beckham 2023 年 12 月 21 日
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:

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by