help-octave
[Top][All Lists]
Advanced

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

Re: Is fprintf slow?


From: John Eaton
Subject: Re: Is fprintf slow?
Date: Mon, 27 Mar 95 14:43:55 CST

address@hidden (Ted Harding) wrote:

: Is fprintf in Octave intrinsically slow?

[...]

: Here is the nub of the script which I use to write a matrix X with R rows
: and C columns of numbers to an ASCII file FIL in format FMT
: with space-separated numbers:
: 
:     S=size(X); R=S(1); C=S(2);

You can write this a bit more compactly as:  [R, C] = size (X);

:     fopen(FIL,"a");
:     for i=1:R
:       for j=1:C
:         fprintf(FIL,FMT,X(i,j)), fprintf(FIL,' '), end, fprintf(FIL,'\n')
:     end

I suspect that the nested for loop accounts for a significant amount
of the time.

: The above timing was for a 10,000 x 2 matrix with FMT="%8.5g", so

[...]

: I'm more interested in factors of the order of 50-100!
: 
: Here is comparison with timings for "save" in the various builtin formats:
: written as above: 433    secs
: save -text:        48.6  secs
: save -binary:       0.15 secs
: save -mat-binary:   0.11 secs

Using save -binary or -mat-binary results in a single call to write()
to dump the 160k in your matrix, so that is very fast.

Using save -ascii (did you really use -text?) results in a loop like

  for (int i = 0; i < a.rows (); i++)
    {
      for (int j = 0; j < a.cols (); j++)
        os << " " << a.elem (i, j);
      os << "\n";
    }
  return os;

I am surprised that this takes 43 seconds on your system.  On my
SPARCstation 2, it only takes about 5.  It takes much less if I set
the variable save_precision to 5 instead of the default 17.

On the same machine, your for loop solution takes about 120 seconds.

I think what is really needed is a way to convert an entire matrix
using a single call to fprintf.  Matlab does this by repeatedly
applying the format string until there is no more data.  All elements
of a matrix argument are converted in column major order.  For example,

  a = rand (3, 2); b = [1, 2; 3, 4; 5, 6];
  fprintf ("%10f  %10e  %10g\n", a);

produces

  0.328234  6.326386e-01     0.75641
  0.991037  3.653387e-01    0.247039
  1.000000  3.000000e+00           5
  2.000000  4.000000e+00           6

Given the current implementation, it would be fairly difficult to make
Octave's fprintf work this way because it is designed to match the
format specifiers with the arguments and not with the matrix elements
that they refer to.

One possiblity that that would be easy to implement and might cover
many uses for something like this would be to introduce some new
format specifiers that work on matrices.  For example, 

  printf (" %8.5G", m)

might print all elements of the matrix m with the format " %8.5g",
separating rows with new lines.

However, this would have the limitation of not allowing you to specify
different formats for different columns in a matrix.

Thanks,

jwe



reply via email to

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