Why should mvnpdf function be too slow

조회 수: 5 (최근 30일)
Boris
Boris 2012년 3월 14일
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.

채택된 답변

Oleg Komarov
Oleg Komarov 2012년 3월 14일
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
Boris
Boris 2012년 3월 14일
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 2012년 3월 14일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Peter Perkins
Peter Perkins 2012년 3월 14일
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
Oleg Komarov
Oleg Komarov 2012년 3월 14일
Didn't know about the "initialization" of the JIT (although sounds logical). Is there any technical documentation about it?
Walter Roberson
Walter Roberson 2012년 3월 14일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by