Reputation: 5805
Is log(a*b) always faster in Matlab than log(a) + log(b)?
I tested for several inputs and it seems log(a*b) is faster. Can you more experienced guys give me some opinions on this? Maybe warnings that this might not always be the case, or something else I should be careful with? So in the first case we have 1 log operation and 1 multiplication, in the second case we have two log operations and one summation.
Edit:
To add to my original post, the more general question is:
is log (a*b*...*z) always faster than log(a) + log(b) + ... + log(z)?
Thanks
Upvotes: 5
Views: 773
Reputation: 20915
Beware, while calculating log
of product is faster, it can be sometimes incorrect due to machine precision.
One of the problematic cases is using a lot of integer operands or large numbers as operands. In this case, the product a_1 * a_2 * ... a_n
will result in an overflow, while computing the sum of logarithms will not.
Another problematic case is using small numbers such that their product becomes zero due to machine precision (As was mentioned by Amro).
Upvotes: 9
Reputation: 21563
Though it will usually be faster to do log(a*b)
instead of log(a) + log(b)
this does not hold if it is hard to evaluate a*b
. In this case it can actually be the case that it is faster to use the second method.
Example:
a = 0;
b = Inf;
tic,for t = 1:1e6 log(a*b); end,toc
tic,for t = 1:1e6 log(a)+log(b); end,toc
Of course it will evaluate to NaN
in both cases, but the second one is considerably faster than the first one.
Upvotes: 2
Reputation: 17026
log(a*b)
should always be faster, because computing the logarithm is expensive. In log(a*b)
you only do it once, in log(a)+log(b)
you do it twice.
Computing products and sums is trivial compared to logarithms, exponentials etc. In terms of processor cycles, both sums and products are generally less than 5 whereas exponentials and logarithms can go from 50 up to 200 cycles for some architectures.
Is log (a*b*...*z) always faster than log(a) + log(b) + ... + log(z)
Yes. Definitely. Avoid computing logarithms whenever possible.
Here's a small experiment:
a=rand(5000000,1);
% log(a(1)*a(2)...)
tic
for ii=1:100
res=log(prod(a));
end
toc
% Elapsed time is 0.649393 seconds.
% log(a(1))+log(a(2))+...
tic
for ii=1:100
res=sum(log(a));
end
toc
% Elapsed time is 6.894769 seconds.
At some point the ratio in time will saturate. Where it saturates depends on your processor architecture, but the difference will be at least an order of magnitude.
Upvotes: 13