help-octave
[Top][All Lists]
Advanced

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

Re: How to efficiently write formatted file output ...


From: Thomas D. Dean
Subject: Re: How to efficiently write formatted file output ...
Date: Fri, 17 Jun 2016 12:36:09 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.8.0

On 06/17/2016 11:51 AM, Dr.-Ing. Dieter Jurzitza wrote:
Dear listmembers,
I wrote a function in octave that basically does what I want it to. However, I
am facing speed issues.

My function contains a loop to write it's results into a file - formatted ...
please see the following example-code:

  tic
  j=linspace(0,99999,100000);
  outfile=fopen("/home/fred/work/Software/octave/test/Spektrum.dat", "w");
  fprintf (outfile, "#\n# Nr.  Spektrum, Zeitfunktion  Eingangssignal\n#\n");
  for i=1:100000
     fprintf(outfile, "%d %12.10f %12.10f %12.10f\n", j(i), s(i), B(i), A(i));
  endfor
  toc

Please consider the arrays to be filled as intended. The elapsed time is said
to be 4.2 seconds. As you might see from this, I grew up programming "C"
language :-)

Now I played a little with fprintf, bound to fail, however this:

  tic
  j=linspace(0,99999,100000);
  outfile=fopen("/home/fred/work/Software/octave/test/Spektrum.dat", "w");
  fprintf (outfile, "#\n# Nr.  Spektrum, Zeitfunktion  Eingangssignal\n#\n");
  fprintf(outfile, "%d %12.10f %12.10f %12.10f\n", j, s, B, A);
  toc

doesn't do the right thing, as the arrays are treated one after the other,
leading to a strange looking output file, however the elapsed time goes down
to 0.38s or 1/10th of the time used for the "for" loop. All data I want to
write to the file are in there, but you know, the order of the data is not
what I hoped it to be.

I am not a speed junky, but a factor of 10 is something significant IMHO. This
for loop is the element in my program lasting longer than anything else. Does
anybody know a more efficient way to write my data to the output file without
such a huge loss in performance?

Many thanks for any suggestion in advance,
take care




Dieter Jurzitza


Concatenate j, s, B, A, like C = [j',s',B',A'];
Then, use fprintf("...",C');  ## I think this is correct.  Make a test case.

j=[1:5]; s=10*j; B=10*s; A=10*B;
C = [j',s',B',A'];
printf("%5d - %5d = %5d ? %5d\n",C')

Tom Dean




reply via email to

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