# Vectorized code is slower than for loop in Matlab

I have an 8x8 matrix called gimg. I have executed this code for 5 different gimg matrices with this code, one vectorized, the other in a for loop.

``````tic
dm = zeros(size(gimg));

for x = 1:size(gimg, 1)
for y = 1:size(gimg, 2)
dm(x, y) = (1/(1 + (x - y)^2))*gimg(x,y);
end
end
toc

tic
[x,y] = ndgrid(1:size(gimg, 1),1:size(gimg, 2));

dm = (ones(size(gimg))./(1 + (x - y).^2)).*gimg;
toc
```

```

Here are the results,

``````Elapsed time is 0.000057 seconds.
Elapsed time is 0.000247 seconds.

Elapsed time is 0.000062 seconds.
Elapsed time is 0.000199 seconds.

Elapsed time is 0.000056 seconds.
Elapsed time is 0.000195 seconds.

Elapsed time is 0.000055 seconds.
Elapsed time is 0.000192 seconds.

Elapsed time is 0.000056 seconds.
Elapsed time is 0.000187 seconds.
```

```

Is it because of the matrix?

I find that speeding up a function in Matlab drastically changes the times for the loops. So my question is, is it worth it now to vectorize the code using these functions from the JIT compiler?

UPDATE: this is one example of my gimg matrix

``````gimg =

259          42           0           0           0           0           0           0
42        1064          41           0           0           0           0           0
0          55        3444         196           0           0           0           0
0           0         215        3581          47           0           0           0
0           0           0         100         806           3           0           0
0           0           0           0           3           2           0           0
0           0           0           0           0           0           0           0
0           0           0           0           0           0           0           0
```

```

UPDATE 2: Results from @ Divakar's code

``````>> test_vct
------------------------ With Original Loopy Approach
Elapsed time is 5.269883 seconds.
------------------------ With Original Vectorized Approach
Elapsed time is 6.314792 seconds.
------------------------ With Proposed Vectorized Approach
Elapsed time is 3.146764 seconds.
>>
```

```

So, on my computer, the original vector approach is still slower.

My computer specs and Matlab version

• Matlab 2015a
• Windows 8.1 x64
• Intel i7 860 2.80 Ghz
• RAM 16 GB
• Nvidia Geforce GTS250
+3

source to share

This seems to be faster than both of them -

``````dm = (1./(1+bsxfun(@minus,[1:size(gimg, 1)]',1:size(gimg, 2)).^2).*gimg);
```

```

Benchmarking code -

``````%// Random input
gimg = rand(8,8);

%// Number of trials (keep this a big number, as so to get runtimes of 1sec+
num_iter = 100000;

disp('------------------------ With Original Loopy Approach')
tic
for iter = 1:num_iter
dm = zeros(size(gimg));
for x = 1:size(gimg, 1)
for y = 1:size(gimg, 2)
dm(x, y) = (1/(1 + (x - y)^2))*gimg(x,y);
end
end
end
toc

disp('------------------------ With Original Vectorized Approach')
tic
for iter = 1:num_iter
[x,y] = ndgrid(1:size(gimg, 1),1:size(gimg, 2));
dm2 = (ones(size(gimg))./(1 + (x - y).^2)).*gimg;
end
toc

disp('------------------------ With Proposed Vectorized Approach')
tic
for iter = 1:num_iter
dm3 = (1./(1+bsxfun(@minus,[1:size(gimg, 1)]',1:size(gimg, 2)).^2).*gimg);
end
toc
```

```

Results -

``````------------------------ With Original Loopy Approach
Elapsed time is 4.996531 seconds.
------------------------ With Original Vectorized Approach
Elapsed time is 2.684011 seconds.
------------------------ With Proposed Vectorized Approach
Elapsed time is 1.338118 seconds.
```

```
+4

source

All Articles