Why is betapdf so slow?

1 view (last 30 days)
William Wooley
William Wooley on 9 Dec 2015
Commented: Jeff Miller on 19 Jul 2020
I'm running some code where I need to evaluate several beta pdfs per iteration. When I profile my code I notice that the four calls to betapdf consume about one-third (0.012s out of 0.03s total) of the run-time.
It seems that betapdf is implemented as an .m file rather than mex. Is there a mex'ed (or just faster) version of betapdf out there?
  2 Comments
James Heald
James Heald on 17 Jul 2020
i also need a faster version of this. it is prohibitively slow for the arrays im working with. it is about 10 x slower than normpdf
Jeff Miller
Jeff Miller on 19 Jul 2020
Cupid is a bit faster if the distribution parameters are fixed:
n = 1000000;
a = 3;
b = 2;
x = betarnd(a,b,n,1);
tic
xpdf = betapdf(x,a,b);
toc
% Elapsed time is 0.306104 seconds.
cupidbeta = Beta(a,b); % Using Cupid
tic
xpdfcupid = cupidbeta.PDF(x);
toc
% Elapsed time is 0.090670 seconds.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 9 Dec 2015
betapdf is as it is, because it is. Writing it as an m-file versus a mex-ed file has advantages, but also disadvantages. Yes, were MATLAB composed of entirely compiled code, those compiled pieces would usually run faster. At the same time, m-code is easier to read, easier to debug, easier to change if at some point in the future they wanted to do something.
Since I would expect that few people are that worried about the brute speed of beta pdf evaluation, they chose to leave it in an m-file form.

Categories

Find more on Polynomials in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!