From: Chen on
While profiling one of my programs I noticed that Log10 calculations were taking a relatively long time. I was surprised to find that it was a bit slower than Log2, which in itself was considerably slower than Log. This can be seen here: (source code attached)
http://img231.imageshack.us/img231/4461/logtimes.png

First, I'd be interested to know why this is so, and second I would like suggestions how to speed up the execution time of Log10 (I have to use it in my program...). I'm using Matlab 7.7.0 R2008b.

Thanks,
Chen

-----------------------

f = 1:1e6;
iters = 1000;

log2_times = zeros(1,iters);
log_times = zeros(1,iters);
log10_times = zeros(1,iters);

for i = 1:iters
tic;
log2(f);
log2_times(i) = toc;

tic;
log(f);
log_times(i) = toc;

tic;
log10(f);
log10_times(i) = toc;

i
end

figure;
hold all;
[n,xout] = hist(log10_times,30);
bar(xout,n);
[n,xout] = hist(log2_times,30);
bar(xout,n,'g');
[n,xout] = hist(log_times,30);
bar(xout,n,'r');
legend('Log10', 'Log2', 'Log');
xlabel('Runtime');
ylabel('Counts');
From: Matt Fig on
I modified your testing program slightly.

function [] = log_time()
f = 1:1e6;
iters = 50;
T = [0 0 0];

for ii = 1:iters
tic;
log2(f);
T(1) = T(1) + toc;

tic;
log(f);
T(2) = T(2) + toc;

tic;
log10(f);
T(3) = T(3) + toc;
end

reltimes = T./min(T) % Dump relative timings to command window.





I get:

>> log_time
reltimes =
1.5119 1 2.2723


Why LOG2 is slower than LOG I do not know. As for LOG10, it is an M-File whereas the others are compiled code. Try to edit them to see what I mean.
From: James Tursa on
"Chen " <mygiga(a)gmail.com> wrote in message <hpg40b$n7p$1(a)fred.mathworks.com>...
> While profiling one of my programs I noticed that Log10 calculations were taking a relatively long time. I was surprised to find that it was a bit slower than Log2, which in itself was considerably slower than Log. This can be seen here: (source code attached)
> http://img231.imageshack.us/img231/4461/logtimes.png
>
> First, I'd be interested to know why this is so, and second I would like suggestions how to speed up the execution time of Log10 (I have to use it in my program...). I'm using Matlab 7.7.0 R2008b.

From the comments in log10 it appears that MATLAB is doing extra work in this function in order to make answers consistent with expectations with regards to roundoff and inverse functions. Presumably the same is true for log2. If you are not worried about that least significant bit being a bit off (pardon the pun) for consistency then you could always define functions based on this:

mylog10(x) = log(x) / log(10)
mylog2(x) = log(x) / log(2)

These still do more work than just the log( ) function by itself, but they are faster than the built-in functions.

James Tursa