help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Structures and tensors


From: Peter Cloetens
Subject: Re: Structures and tensors
Date: Wed, 2 Aug 2006 10:36:01 +0200

Hi,
I had similar effects with slow code containing a lot of 3-dimensional arrays. Then I rewrote everything with cells, where each element of the cell array corresponds to 1 matrix. This improved the speed very considerably. 
You use:
E{k}  instead of E(:,:,k)
As long as you do not need to access your data in the third dimension this is much more efficient and readable.
Peter

On Aug 1, 2006, at 5:55 PM, Cédric Févotte wrote:

Hi everyone,

What are the general recommandations about the use of structures and 
tensors in Octave ? Understand, is it recommanded to avoid them ?

What led to my question is the following piece of code, in which I copy 
the vector ones(n,1) sequentially into a variable, which is either
1) a matrix of size n x n_iter,
2) a matrix of size n x n_iter contained in the field of a structure,
3) a tensor of size n x n_iter x 1,
4) a tensor of size n x n_iter x 1 contained in the field of a structure

The results are striking (obtained either on octave 2.1 or 2.9 under 
Linux Debian) :

Elapsed time is 0.071320 seconds.
Elapsed time is 0.324188 seconds.
Elapsed time is 3.279373 seconds.
Elapsed time is 3.327034 seconds.

Any comments ?

All the best

Cedric

%%% Code %%%

n_iter = 10000;
n=50;

%%% 1: Matrix of size n x n_iter %%%
E=zeros(n,n_iter);

tic
for iter=1:n_iter
   E(:,iter)=ones(n,1);
end
toc

%%% 2: Matrix in Structure %%%
cur = struct("E",zeros(n,n_iter));

tic
for iter=1:n_iter
   cur.E(:,iter)=ones(n,1);
end
toc

%%% 3: Tensor of size n x n_iter x 1  %%%
E=zeros(n,n_iter,1);

tic
for iter=1:n_iter
   E(:,iter,1)=ones(n,1);
end
toc

%%% 4: Tensor in Structure %%%
cur = struct("E",zeros(n,n_iter,1));

tic
for iter=1:n_iter
   cur.E(:,iter,1)=ones(n,1);
end
toc

%%% End Code %%%

-- 
Dr Cédric Févotte
Researcher
Mist Technologies
204 rue de Crimée, 75019 Paris, France
Tel: +33 1 55 26 42 31 - Fax: +33 1 55 26 42 03
_______________________________________________
Help-octave mailing list



Peter Cloetens
ESRF
BP 220
F-38043 Grenoble
tel  : + 33 (0)4 76 88 26 50
fax : + 33 (0)4 76 88 27 85



reply via email to

[Prev in Thread] Current Thread [Next in Thread]