Multi-window app efficiency, should I use public or private variables & functions?

2 ビュー (過去 30 日間)
Hi Matlab Community
I'm currently working on optimizing the performance of a multi-window app with many different components. I intended to let many people use the app so I'm trying to optimize the code to make it as efficient as possible. Here's some of the quesitons I have regarding code efficiency.
  • When passing large variable between different compoent between apps (such as a big matrix), is it more effiencient to create a public variable in one of the app, and then acess the variable by refering to the variable as "app.calling_app.variable_name", or is it better to use create a private variable and then pass the variable to the other app's start up function
  • Similary, when a funcition is used in multiple apps, will it be better to 1.) create the funciton as a public function in one of the app, and calls the public function in other apps, or 2.) create multiple identical private funcitons in each app, or 3.) to put the functions in a .m file?
  • Finally, since all the funcition in app designer seems to start with the input variable app, when the funtion need to use private variable, is it better also pass the variable as a input variable (i.e. function(app,variable) ), or is it better just to pass app as the only input variable(i.e function(app)), and reference to the variable as app.variable within the function
Thanks a lot for your help!
  2 件のコメント
Rik
Rik 2021 年 10 月 9 日
I never work with AppDesigner, so I don't have an actual answer, but I do have a few thoughts.
Normally all variables in Matlab are pass by reference, meaning a new copy of the data will only be made when the variable changes. That means passing large data structures does not incur a penalty.
I personally prefer to have functions exist only in one place and call the same function from different places. That way I have only a single location to update.
I don't know how invested you are in this setup, but if you can still change your design: for general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
Guandong Wang
Guandong Wang 2021 年 10 月 10 日
Thanks for your comments, its good to know that passing varialbe doesn't affect the efficiency. At the moment the project is almost finished so I would like to keep using app designer, but Its good to know the pros and cons for each method of creating a GUI, thanks a lot!

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

採用された回答

Walter Roberson
Walter Roberson 2021 年 10 月 10 日
I did some timing tests in https://www.mathworks.com/matlabcentral/answers/898187-what-is-the-alternative-function-for-evalin#comment_1689662 but I did not check the efficiency of struct and object fields. Let us extend...
... The timings seem to imply that object field access is significantly slower than struct or passing a plain variable.
I cannot construct a new class in this online facility.
format long g
test_base();
t = 5×1
0.0042 0.0076 0.0030 0.0067 0.0030
function test_base
N = 500;
A = rand(1e7,1);
assignin('base', 'A', A);
A_obj = line(nan,nan,'UserData', A);
A_struct = struct('A', A);
t = zeros(5,1);
tic; for K = 1:N; via_evalin1(); end; t(1) = toc;
tic; for K = 1:N; via_shared1(); end; t(2) = toc;
tic; for K = 1:N; via_passed1(A); end; t(3) = toc;
tic; for K = 1:N; via_passed_obj1(A_obj); end; t(4) = toc;
tic; for K = 1:N; via_passed_struct1(A_struct); end; t(5) = toc;
t
cats = categorical({'evalin', 'shared variable', 'passed parameter', 'passed object', 'passed struct'});
bar(cats, t);
function B = via_evalin1()
B = via_evalin2();
end
function B = via_evalin2()
B = via_evalin3();
end
function B = via_evalin3();
B = via_evalin4();
end
function B = via_evalin4();
B = via_evalin5();
end
function B = via_evalin5();
B = evalin('base', 'A');
end
function B = via_shared1()
B = via_shared2();
function B = via_shared2()
B = via_shared3();
function B = via_shared3();
B = via_shared4();
function B = via_shared4();
B = via_shared5();
function B = via_shared5();
B = A;
end
end
end
end
end
function B = via_passed1(A)
B = via_passed2(A);
end
function B = via_passed2(A)
B = via_passed3(A);
end
function B = via_passed3(A);
B = via_passed4(A);
end
function B = via_passed4(A);
B = via_passed5(A);
end
function B = via_passed5(A);
B = A;
end
function B = via_passed_struct1(AS)
B = via_ps2(AS);
end
function B = via_ps2(AS)
B = via_ps3(AS);
end
function B = via_ps3(AS);
B = via_ps4(AS);
end
function B = via_ps4(AS);
B = via_ps5(AS);
end
function B = via_ps5(AS);
B = AS.A;
end
function B = via_passed_obj1(AO)
B = via_po2(AO);
end
function B = via_po2(AO)
B = via_po3(AO);
end
function B = via_po3(AO);
B = via_po4(AO);
end
function B = via_po4(AO);
B = via_po5(AO);
end
function B = via_po5(AO);
B = AO.UserData;
end
end

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by