필터 지우기
필터 지우기

Generate Code for classes methods

조회 수: 36 (최근 30일)
Net Fre
Net Fre 2024년 7월 9일 8:55
답변: Pratyush 2024년 7월 11일 8:42
Hi.
This is more of a clarification question than a "how to", because I got the MATLAB answer and it works, but I have a question about it.
I have a class with several properties and methods. I would like to use code generation to make my script, which uses the class, run faster (specifically for now - using the .mex function). I saw that the solution for generating code from a class method is creating a function that constructs the class, and then calls the method, as described here.
My question is - doesn't it make things much less efficient? the code now, instead of calling just the method itself each time, the function needs to create the class just to call it - every time I want to use it. It becomes even more of a problem if I want to use several methods of the same class, each creating the class every time I call it.
Am I missing something? Is there a more efficient way to do this?
Thank you very much!

답변 (1개)

Pratyush
Pratyush 2024년 7월 11일 8:42
Hi,
Creating a class instance every time you want to call a method can indeed introduce overhead and reduce efficiency. You can try out the following to optimize the process:
  • Use a persistent variable to store the class instance.
  • This avoids the overhead of creating the class every time a method is called.
  • If your class inherits from 'handle', it can be modified without needing to recreate it, which can be more efficient.
  • Utilize directives like 'coder.varsize' and 'coder.inline' to help MATLAB optimize the code generation process.
Hope this helps.

카테고리

Help CenterFile Exchange에서 MATLAB Algorithm Acceleration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by