Why should mvnpdf function be too slow

8 views (last 30 days)
Hello!
The short question: I was translating some Gauss code and I wrote a function to calculate the value of the normal likelihood, which turns out to be 2 times faster than the built in `"mvnpdf". Why should that be?
The details: I wrote the following function first:
function [val] = v_probVec(ev, he)
%calculates the value of the conditional density
%i.e. f(y_t | S_t, S_{t-1}, y_{t-1})
val=(1/(2*pi*sqrt(det(he))))*exp(-0.5*(ev'/he)*ev);
where ev is data, he is the var-covar matrix, mu=0
I call this function in a loop of 150 iterations, which is in a parent function. As I am using sampling algorithms I was looking for a way to improve my code and was reading about "interpreter" and "compiled functions" and thought - why not use a built in function and found the mvnpdf function.
I did the following speedtest. I call the parent function 1000 times and using tic and toc I got the following results: 43.261172 (max 45) seconds for my f-n. 77.116527 (max 82) seconds for mvnpdf.
This makes no sense to me, shouldn't the built in be much faster than mine?
If there is some natural explanation and it should be so, just tell me. If that is not the case I could see if I am allowed to publish my code.

Accepted Answer

Oleg Komarov
Oleg Komarov on 14 Mar 2012
It's is not a builtin function since it's not compiled but written with MATLAB code.
Type in the command window to see how it's written:
edit mvnpdf
Compare it for example to
edit max
You can also run the mvnpdf in the profile to check where the function is losing time (some checks).
  2 Comments
Boris
Boris on 14 Mar 2012
Ah, I didn't know that. Thanks! I didn't know I could check that way, I just assumed it was built-in. I'll do so regularly now. mvnpdf is more than 100 lines, no wonder it takes years to compile, compared to my one-liner.
Oleg Komarov
Oleg Komarov on 14 Mar 2012
MATLAB is a functional language, which means that the code is read interpreted and executed on the go, no compilation occurs.
Intuitively, pre-compiled code as built-in functions should be faster.

Sign in to comment.

More Answers (1)

Peter Perkins
Peter Perkins on 14 Mar 2012
As Oleg points out, MVNPDF is written in the MATLAB language. However, that is not to say that it is not "compiled". If you run your timing twice in a fresh MATLAB session, you will most likely find that the first timing run takes longer than subsequent runs because MATLAB's JIT has to process MVNPDF. The subsequent timings are the "real" ones.
As Oleg also points out, you can open MVNPDF in the editor. As you will see, MVNPDF has a lot of error checking and generality. If you are writing code that doesn't need that, you can write a shorter version based on what you see in MVNPDF.
You don't say how you are calling MVNPDF, but the more vectorized you code is, the less that error checking overhead will affect the execution time. For example, MVNPDF accepts a matrix as its first input, and returns a vector of probability density values. If you can take advantage of that, you should.
Hope this helps.
  2 Comments
Oleg Komarov
Oleg Komarov on 14 Mar 2012
Didn't know about the "initialization" of the JIT (although sounds logical). Is there any technical documentation about it?
Walter Roberson
Walter Roberson on 14 Mar 2012
When a _function_ is required and is not already in memory, it will be processed by the JIT and the threaded-interpreted code will be stored in memory, along with the source (for debugging.) If the source is then modified within the MATLAB editor, MATLAB will "clear" the JIT'd version when the source is saved (leaving the code to be JIT'd again the next time it is needed.) If the source is modified through some other mechanism, such as an outside editor, or through the program writing a new version of the .m file, then MATLAB will not usually notice the change and will continue to execute the JIT'd version unless you specifically "clear" the function.

Sign in to comment.

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!