Impact of functions on calculations time!

4 views (last 30 days)
Alireza
Alireza on 10 Sep 2014
Edited: Adam on 10 Sep 2014
First I apologize for my poor English, I have a question. It's important for me. My program in Matlab is too slow! I didn't use function in my program. do function reduce calculations time? or increase it? Thanks.

Accepted Answer

Adam
Adam on 10 Sep 2014
Edited: Adam on 10 Sep 2014
Refactoring the exact same code into functions should have next to no impact on the speed. If it does it would think it would be on the side of a slight decrease in speed.
If you are using classes then there is an overhead to class scope resolution for methods and properties so you have to be a little bit careful there not to slow your program down massively, but I doubt that is relevant in your case.
Using functions is definitely desirable though in general and would likely make it easy to run the profiler and understand and isolate where your code is slow (especially, for example, being able to time just a single function of your code rather than always the whole program at once).
If you are talking about using built-in functions to replace code you have written yourself then certainly they may make the code either faster or slower. It is impossible to say in a general case.

More Answers (1)

Naeem Roshan
Naeem Roshan on 10 Sep 2014
Functions increase performance of the codes and reduce the run time. Its better to split large codes into functions. There are also other things you should consider when writing a MATLAB code in order to increase the performance. For instance avoiding loops, preallocating, vectorization and etc.

Community Treasure Hunt

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

Start Hunting!