Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
The loop is very slow on 64-bits

Subject: The loop is very slow on 64-bits

From: Pablo

Date: 12 Mar, 2012 17:43:10

Message: 1 of 5

Hi all,
I have this problem and I’ts possible my fault… maybe, I forget to change a flag or something like that.
I run this code in my PC (Windows 7 64 bits, Intel Core i7 CPU) using MATLAB R2009a (64bits)
img=imread(‘sample.bmp’);
img=rgb2gray(img) ;
[u,v] = size(img);
ii = double(zeros(u+1,v+1));
s = [zeros(1,v+1) ; zeros(u,1) double(img)];
tic
for i=2:u+1,
    for j=2:v+1,
        s(i,j)=s(i,j-1)+img(i-1,j-1);
        ii(i,j)=ii(i-1,j)+s(i,j);
    end
end
toc
% ii = integral image

The result was:
“Elapsed time is 0.508716 seconds.”

In fact, It’s a lot of time for a 420x560 pixels picture…
In the same PC, I installed the same MATLAB in 32 bits. This time the output was:
“Elapsed time is 0.016215 seconds.”

I couldn’t figure out what I’m doing wrong… I want to continue using MATLAB with 64 bits, but with this performance I can’t wait all the time for the integral image is calculated…

Thanks for your help.

Pablo

Subject: The loop is very slow on 64-bits

From: Roger Stafford

Date: 12 Mar, 2012 18:53:12

Message: 2 of 5

"Pablo" wrote in message <jjlcje$nlk$1@newscl01ah.mathworks.com>...
> Hi all,
> I have this problem and I’ts possible my fault… maybe, I forget to change a flag or something like that.
> I run this code in my PC (Windows 7 64 bits, Intel Core i7 CPU) using MATLAB R2009a (64bits)
> img=imread(‘sample.bmp’);
> img=rgb2gray(img) ;
> [u,v] = size(img);
> ii = double(zeros(u+1,v+1));
> s = [zeros(1,v+1) ; zeros(u,1) double(img)];
> tic
> for i=2:u+1,
> for j=2:v+1,
> s(i,j)=s(i,j-1)+img(i-1,j-1);
> ii(i,j)=ii(i-1,j)+s(i,j);
> end
> end
> toc
> % ii = integral image
>
> The result was:
> “Elapsed time is 0.508716 seconds.”
>
> In fact, It’s a lot of time for a 420x560 pixels picture…
> In the same PC, I installed the same MATLAB in 32 bits. This time the output was:
> “Elapsed time is 0.016215 seconds.”
>
> I couldn’t figure out what I’m doing wrong… I want to continue using MATLAB with 64 bits, but with this performance I can’t wait all the time for the integral image is calculated…
>
> Thanks for your help.
>
> Pablo
- - - - - - - -
  In the line

 s(i,j)=s(i,j-1)+img(i-1,j-1);

you are overwriting the copy of 'img' you originally place in 's' and one wonders why. More importantly each of the u*v times it is executed it has to convert img(i-1,j-1) to double. Perhaps that takes longer with a 64-bit version. Why don't you use the already computed double version of 'img' in that line? It looks to me as though you originally intended to write that line as

 s(i,j)=s(i,j-1)+s(i,j);

which ought to produce the same result, given the 's' as you have set it up earlier.

Roger Stafford

Subject: The loop is very slow on 64-bits

From: Pablo

Date: 13 Mar, 2012 13:46:17

Message: 3 of 5

Dear Roger,
Thanks for the advice.
Indeed, the elapsed time speeds up:
"Elapsed time is 0.435504 seconds."
But, it's still far away from the performance of the 32 bits.
I was wondering if anyone could reproduce this on their PC...
Best regards,
Pablo

Subject: The loop is very slow on 64-bits

From: Bruno Luong

Date: 13 Mar, 2012 19:14:12

Message: 4 of 5

"Pablo" wrote in message <jjlcje$nlk$1@newscl01ah.mathworks.com>...
> Hi all,
> I have this problem and I’ts possible my fault… maybe, I forget to change a flag or something like that.
> I run this code in my PC (Windows 7 64 bits, Intel Core i7 CPU) using MATLAB R2009a (64bits)
> img=imread(‘sample.bmp’);
> img=rgb2gray(img) ;
> [u,v] = size(img);
> ii = double(zeros(u+1,v+1));
> s = [zeros(1,v+1) ; zeros(u,1) double(img)];
> tic
> for i=2:u+1,
> for j=2:v+1,
> s(i,j)=s(i,j-1)+img(i-1,j-1);
> ii(i,j)=ii(i-1,j)+s(i,j);
> end
> end
> toc
> % ii = integral image

Try:

tic
img = double(img);
[u,v] = size(img);
s = [zeros(1,v+1) ; zeros(u,1) cumsum(img,2)];
ii = [zeros(1,v+1) ; zeros(u,1) cumsum(img,1)];
toc

% Bruno

Subject: The loop is very slow on 64-bits

From: Pablo

Date: 16 Mar, 2012 14:33:22

Message: 5 of 5

Bruno,
did you mean:
tic
img = double(img);
[u,v] = size(img);
s = [zeros(1,v+1) ; zeros(u,1) cumsum(img,2)];
ii = cumsum(s,1);
toc
?
"Elapsed time is 0.003878 seconds."

That was fast...

Thanks Bruno.
Regards,

Pablo

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us