« Earlier2 items total Later »

Indexing speed of arrays with different index data types

Here is a simple test to prove that integers are
faster than singles or doubles for indexing matlab arrays.

%% test indexing
function untitled

     function test(type)
        % eg : int32(floor(rand(1,100)*100+1))
         k = eval(sprintf('%s(floor(rand(1,100)*100 + 1))', type));
         x = rand(1,100);
         tic
         for i = 1:1e5
             y = x(k);
         end
         toc
         fprintf('%s\n', type);
     end

     test('int32');
     test('uint32');
     test('single');
     test('double');

end

The results are
Elapsed time is 0.194030 seconds.
int32
Elapsed time is 0.195234 seconds.
uint32
Elapsed time is 0.413456 seconds.
single
Elapsed time is 0.804347 seconds.
double


Thus indexing with integers is definately faster.

Vectorize operations based on column and row indicies

Vectorize this
for i = 1:100
  for j = 1:100
    r(i,j) = sqrt(i^2+j^2);
  end
end


becomes

[i,j]=meshgrid(1:100,1:100);
r = sqrt(i.^2+j.^2);

« Earlier2 items total Later »




Sponsored by

Sole Central

Your one stop shop for Birkenstock and Crocs shoes and sandles.