[Top][All Lists]

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

Why is sscanf() so slow?

From: John W. Eaton
Subject: Why is sscanf() so slow?
Date: Mon, 24 Mar 2003 18:33:14 +0100

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).


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

Octave's home on the web:
How to fund new projects:
Subscription information:

reply via email to

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