Are Matlab's built in functions typically faster than user created functions?

18 views (last 30 days)
Matlab has many functions that can be replicated by users such as mean(). Is the use of these built-in functions faster than writing my own script for them in regard to clock time? Or do these functions just save space while coding to be more efficient?

Answers (1)

dpb
dpb on 5 Jul 2017
Edited: dpb on 5 Jul 2017
Generally the builtins will be faster owing to that they're compiled code as opposed to m-files which are interpreted. Although the JIT compiler does a good job on loops and other expressions, it still isn't the same as writing code for an optimizing compiler and using it instead.
Some builtins have enough error checking and other features incorporated that the full benefit isn't realized as much as one might think; this is particularly true for smaller array sizes where the calling overhead and error checking may be a significant fraction of the total run time. Larger arrays will tend to show the benefit more and timing tests to try to quantify such results would need to include such effects.
The other difference where user code may beat builtins is that TMW-supplied functions are generally very flexible on what they do with various inputs such as vector, matrix, array, etc., operating by default by row but with optional inputs to select that dimension, etc., etc., ... Writing a specific case inline eliminates the extra overhead of that extended capability and consequently may be faster than the compiled code for that particular case. Of course, you then have to rewrite that function for the alternate case(s) when they arise which is a different time cost.
Not sure in most recent releases, but the example chosen of mean is an m-file still on the release here (R2014b) is it now actually a builtin?
ADDENDUM Just dawned on me right after posting--
Unless, of course, by "builtin" you mean any function that is supplied by TMW as part of the product. "Insider" definition would limit that to those that are compiled:
>> type sum
'sum' is a built-in function.
>> which sum
built-in (C:\ML_R2014b\toolbox\matlab\datafun\@uint8\sum) % uint8 method
>> which mean
C:\ML_R2014b\toolbox\matlab\datafun\mean.m
>>
For that definition, then the answer is that all m-files are the same; there's nothing magic in those that TMW wrote vis a vis yours other than the items mentioned above -- they typically are more robust in error checking, etc., and they do have the techniques for multiple array sizes, data types, and potentially other objects as well. Consequently, it may not be terribly hard to "beat" a supplied m-file function if you restrict yours to only default double and a given input such as a vector. By the time you build the full capability into it as its native sister, you'll probably be much nearer to neck 'n neck.

Tags

Products

Community Treasure Hunt

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

Start Hunting!