[Top][All Lists]

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

Re: Why is sscanf() so slow?

From: Paul Kienzle
Subject: Re: Why is sscanf() so slow?
Date: Wed, 26 Mar 2003 18:41:35 -0500
User-agent: Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.3a) Gecko/20021212

stefan wrote:
On Mon, 24 Mar 2003, John W. Eaton wrote:

On 25-Jan-2003, stefan <address@hidden> wrote:

| Dear Octaver's,
| first of all:  Thanks for great software.  I am using it now for about two
| years, especialy for displaying and adjusting measured data (comes from
| some measurement bus system to computer).  The software which drives these
| devices almostly always produce tab- or comma-seperated data.
| For this I do:
|     while (isstr(line = fgets(fid)))
|       if (index(line, "#") == 1)
|         continue;
|       endif
|       if (length(line) <= 2)
|         continue;
|       endif
|       numbers = sscanf(line, "%g %g %g %g %g %g %g %g", 8);
|       for i = 1 : length(numbers)
|         values(n, i) = numbers(i);
|       endfor
|       n = n + 1;
|     endwhile
| or very likely...
| For more than 1000 lines this takes *ages*.

Try preallocating the values array.  Assuming you haven't done that,
then the code above resized values many times.  If you are not sure of
the final size, allocate something large, reallocating if necessary
(but in large chunks, perhaps 2x previous allocation, or similary),
then resize when you reach eof.  Sscanf is also slow.  If you just
have numbers in the file, maybe you want to use load instead.  With
current snapshots, it should support command and tab delimited files
with comments (beginning with # or % characters, I think).

Right now I am using a modified version of the 'aload.m' which some on
this mailing list sent to me.

I'll also try your hint and tell you whether it gets better...
There is an oct-file version of dlmread on octave-forge:

Paul Kienzle

reply via email to

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