Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is log(a*b) always faster in Matlab than log(a) + log(b)?

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

like image 411
user2381422 Avatar asked Aug 22 '13 16:08

user2381422


2 Answers

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.

like image 91
Marc Claesen Avatar answered Sep 28 '22 02:09

Marc Claesen


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

like image 35
Andrey Rubshtein Avatar answered Sep 28 '22 03:09

Andrey Rubshtein