help-octave
[Top][All Lists]
Advanced

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

How to efficiently write formatted file output ...


From: Dr.-Ing. Dieter Jurzitza
Subject: How to efficiently write formatted file output ...
Date: Fri, 17 Jun 2016 20:51:38 +0200
User-agent: KMail/4.14.10 (Linux/4.1.21-15.2-default; KDE/4.14.18; x86_64; ; )

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

-- 
-----------------------------------------------------------

                               |
                                \
                 /\_/\           |
                | ~x~ |/-----\   /
                 \   /-       \_/
  ^^__   _        /  _  ____   /
 <°°__ \- \_/     |  |/    |  |
  ||  ||         _| _|    _| _|

if you really want to see the pictures above - use some font
with constant spacing like courier! :-)
-----------------------------------------------------------




reply via email to

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