<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">Hello,<br>
<br>
Le 03/07/2015 09:42, jaipur a écrit :<br>
</div>
<blockquote cite="mid:1435909334253-4032530.post@n3.nabble.com"
type="cite">
<pre wrap="">I'm making a big structure (about 110,000 items) by reading data from
Hipparcos star catalog text file.
My procedure is as follows.
MyStruct = struct(); Index = 0;
while ~meof(Fd) do
........
........
Index = Index + 1;
MyStruct(Index).Field1 = ...;
MyStruct(Index).Field2 = ...;
MyStruct(Index).Field3 = ...;
end
But this procedure takes huge time!!
The number of items is known. Could you teach me speedy way to make big
structure?
For example, keep memory for structure before starting like
MyVector = ones(110000,1);</pre>
</blockquote>
When creating new components of a structures array elements with an
<b>increasing</b> index, the time spent to create each one
increases, and the total time increases in a parabolic way:<br>
<br>
-->clear s, tic; t=[]; for i=1:1000, s(i).n=%pi; if
pmodulo(i,10)==0, t(i/10)=toc(); end, end, toc()<br>
ans = <br>
52.603 <br>
-->plot(t)<br>
<img src="cid:part1.02000108.02070606@free.fr" alt="" width="283"
height="212"><br>
<br>
Now, if components are created with a decreasing index, the time
spent to create each one is constant, and the total time varies in a
linear way:<br>
-->clear s, tic; t=[]; for i=1000:-1:1, s(i).n=%pi; if
pmodulo(i,10)==0, t(i/10)=toc(); end, end, toc()<br>
ans =<br>
5.553 <br>
-->plot(t)<br>
<img src="cid:part2.09080906.01030502@free.fr" alt="" width="270"
height="205"><br>
<br>
Finally, initializing the array to its final size, and then filling
its components at increasing index is also fast and linear in time:<br>
<br>
-->clear s, tic; t=[]; s(1000).n=0; for i=1:1000, s(i).n=%pi; if
pmodulo(i,10)==0, t(i/10)=toc(); end, end, toc()<br>
ans =<br>
5.696 <br>
<img src="cid:part3.08060308.07040606@free.fr" alt="" width="287"
height="216"><br>
<br>
Therefore, in your case, you may initialize:<br>
<pre wrap=""> MyStruct(110000).Field1 = 0;
MyStruct(110000).Field2 = 0;
MyStruct(110000).Field3 = 0;</pre>
and then assign the components as you did, at increasing index.<br>
<br>
HTH<br>
Samuel<br>
<br>
</body>
</html>