MATLAB: Should mvnpdf function be too slow

MATLABmvnpdf

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.

Best Answer

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).