Why is this line so inefficient

I have a very simple equation that I'm coding in MATLAB that I wouldn't think should be taking as much time as it is. The initial line is in a for loop so I vectorized the problem and that turned out to be even slower (which I was pretty surprised by). Does anyone see anything obvious that I'm not considering for why this is so slow? Typically values for n and m are between 0-10
% Rnm = zeros(size(rho));
% MaximumExponent = (n-m)/2;
% s = 0:MaximumExponent;
% numerator = (-1) .^ s .* factorial(n-s);
% denominator = factorial(s) .* factorial((n+m) / 2-s) .* factorial((n-m) / 2-s);
% Rnm = sum((numerator ./ denominator) .* rho .^ (n-2 .* s), 2);
Rnm2 = zeros(size(rho));
for s=0:(n-m)/2
numerator = (-1)^s * factorial(n-s);
denominator = factorial(s)*factorial((n+m)/2-s)*factorial((n-m)/2-s);
Rnm2 = Rnm2 + (numerator / denominator) * rho.^(n-2*s);
end % for s statement
Here is a profile for the runtime...
EDIT: I should probably also add a couple more details on some things. rho will be a column vector of ~500,000 elements. I imagine the size of rho is alone the reason that most of this takes so long, but it still seems a bit abnormally long to me.
EDIT 2: Should probably include my system details...running Windows 10 Pro, 11th Gen Intel Core i5-1135G7 @ 2.4GHz, 16GB RAM. Nothing to fantastic, but not the worst PC in the world. That's why I'd think this should be running faster...

2 Kommentare

dpb
dpb am 26 Jul. 2022
What's rho?
John Gilmore
John Gilmore am 26 Jul. 2022
Please see my edit.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

David Hill
David Hill am 26 Jul. 2022

0 Stimmen

tic;
rho=rand(500000,1);n=9;m=3;
s=0:(n-m)/2;
numerator = (-1).^s .* factorial(n-s);
denominator = factorial(s).*factorial((n+m)/2-s).*factorial((n-m)/2-s);
Rnm=sum((numerator./denominator).*rho.^(n-2*s),2);
toc;
Elapsed time is 0.109334 seconds.

4 Kommentare

John Gilmore
John Gilmore am 26 Jul. 2022
Bearbeitet: John Gilmore am 26 Jul. 2022
Now that's interesting...this is sort of what my question is implying I suppose. for you this takes ~30 seconds for 280 function calls. Which is about 300% faster than mine. Is that drastic of a speed increase really just do to our different systems?
EDIT: See original post EDIT2 for system specs
dpb
dpb am 26 Jul. 2022
Bearbeitet: dpb am 26 Jul. 2022
>> tic;
rho=rand(500000,1);n=9;m=3;
s=0:(n-m)/2;
numerator = (-1).^s .* factorial(n-s);
denominator = factorial(s).*factorial((n+m)/2-s).*factorial((n-m)/2-s);
Rnm=sum((numerator./denominator).*rho.^(n-2*s),2);
toc;
Elapsed time is 0.094052 seconds.
>>
What else do you have in memory? Must be something else going on for it to be more than a fraction of a second with above parameters.
What does
whos
show?
Processor Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz, 3601 Mhz, 4 Core(s), 8 Logical Processor(s)
John Gilmore
John Gilmore am 27 Jul. 2022
While the script isn't running? Running that command shows nothing.
tic;
rho=rand(500000,1);n=9;m=3;
f=factorial(0:n);%could do lookup table for factorials (might help speed slightly)
s=0:(n-m)/2;
numerator = (-1).^s .* f(n-s+1);
denominator = f(s+1).*f((n+m)/2-s+1).*f((n-m)/2-s+1);
Rnm=sum((numerator./denominator).*rho.^(n-2*s),2);
toc;
Elapsed time is 0.102554 seconds.

Melden Sie sich an, um zu kommentieren.

Kategorien

Produkte

Version

R2022a

Gefragt:

am 26 Jul. 2022

Kommentiert:

am 27 Jul. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by