computation efficiency regarding function use

5 views (last 30 days)
Ilija
Ilija on 12 Mar 2011
Hello, I'm designing some FEM, FDM applications in matlab and have a question about computation efficiency -
If I were to apply Gauss Seidel method for solving a system of linear algebraic equations(which I am), is it better to integrate it in form of a function or to integrate the code itself in the program? Does it matter at all?
Thanks

Answers (1)

Jan
Jan on 12 Mar 2011
Calling a function takes some time in Matlab. Therefore inlining the subfunction results in a faster execution.
But the testing and debugging of the algorithm will be more efficient if the calculation is split into different functions. It will not be helpful to reduce the runtime by some seconds, but increase the time for programming and debugging by hours.
  2 Comments
Ilija
Ilija on 14 Mar 2011
Thanks for the effort though I would like to rephraze your answer just to see if I got it right.
From a computation perspective, the difference in efficiency of an integrated code and a function in the code is the function loading time (a few sec) - but in all otehr aspects of speed/efficiency it's pretty much the same.
Thanks again for the advice
Jan
Jan on 14 Mar 2011
No. The function file is loaded and parsed once. This takes less than a second. But each time the function is called, Matlab needs a certain amount of time to start it.
Simply try it:
tic; for i = 1:10000; y = mean(rand(1, 100)); end; toc
tic; for i = 1:10000; y = sum(rand(1, 100))/100; end; toc
Here the direct call of SUM is 3.5 times faster. This has three reasons: 1. MEAN checks the input (this is time consuming, but *very* important: better waste 1 second of runtime that 1 hour for debugging!). 2. Even if yout create a minimal MEAN implementation ("function Y = myMean(X), Y=sum(X)/length(X)") the inlined version is 44% faster. 3. Matlab's JIT acceleration can use an intelligent pre-parsing of the FOR loop, if only built-in function are used inside.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!