help-octave
[Top][All Lists]
Advanced

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

"fwrite" seems very slow


From: John W. Eaton
Subject: "fwrite" seems very slow
Date: Mon, 28 Mar 2005 21:23:17 -0500

On 28-Mar-2005, Edward C. Jones <address@hidden> wrote:

| Why does it take about .76 secs for this octave program to run?
| 
|      a = zeros(256, 256, "double");
|      f = fopen("mess", "wb");
|      fwrite(f, a, "double", [256, 256]);
|      fclose(f);
| 
| The following piece of Python(!) is 300 times faster:
| 
|      s = (256*256*8)*chr(0)
|      f = open('mess', 'wb')
|      f.write(s)
|      f.close()

I don't think the Octave code is doing what you expect.

The fourth parameter to fwrite is SKIP, which is supposed to skip SKIP
bytes before writing each element of the array to the file.  SKIP is
supposed to be an integer, but for compatibility reasons, Octave
allows you to pass a matrix and it simply uses the first element.

Octave will write the file much faster and do the same thing as the
Python code if you omit the SKIP value.

Unfortunately, there also seems to be a bug and Octave is not
skipping properly when you do specify the SKIP parameter.

OTOH, Matlab apparently does not actually skip, but writes SKIP NUL
bytes to the file before writing each element of the array, even when
the file already contains data.  Seems rather broken to me, but here
is a patch that should give compatible behavior.

BTW, when reporting problems (even if you are just asking "hey, has
anyone else seen this behavior?"), it is probably best to give a
little more information, like what platform and version of Octave you
are using, etc.  If you think you have found an actual bug, then it is
best to send a complete report to the address@hidden list.  If you're
not sure what to include in your report so that someone might actually
be able to fix the problem, then please read
http://www.octave.org/bugs.html before sending your report.

Thanks,

jwe


2005-03-28  John W. Eaton  <address@hidden>

        * oct-stream.cc (octave_stream::write): For compatibility, Write
        zeros instead of seeking if SKIP is nonzero.


Index: src/oct-stream.cc
===================================================================
RCS file: /usr/local/cvsroot/octave/src/oct-stream.cc,v
retrieving revision 1.110.2.2
retrieving revision 1.110.2.3
diff -u -r1.110.2.2 -r1.110.2.3
--- src/oct-stream.cc   4 Mar 2005 17:05:43 -0000       1.110.2.2
+++ src/oct-stream.cc   28 Mar 2005 23:38:05 -0000      1.110.2.3
@@ -3471,8 +3471,18 @@
        {
          std::ostream& os = *osp;
 
+         // It seems that Matlab writes zeros instead of actually
+         // seeking.  Hmm...
+
          if (skip != 0 && (i % block_size) == 0)
-           seek (skip, SEEK_CUR);
+           {
+             // XXX FIXME XXX -- probably should try to write larger
+             // blocks...
+
+             unsigned char zero = 0;
+             for (int j = 0; j < skip; j++)
+               os.write (reinterpret_cast<const char *> (&zero), 1);
+           }
 
          if (os)
            {



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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