unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* How to read integers from file faster?
@ 2013-08-31  3:55 Darren Hoo
  2013-08-31  5:46 ` Mike Gran
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Darren Hoo @ 2013-08-31  3:55 UTC (permalink / raw)
  To: guile-user


It is way too slow to read numbers from a file simply by using `read' 

for example a txt file contains 10,000,000 line of numbers:

    (define (gen-sample max k file)
      (with-output-to-file 
          file
        (lambda ()
          (let lp ((k k))
    	(when (> k 0)
    	  (display (random max)) (newline)
    	  (lp (- k 1) ))))))
         
    (gen-sample 999999999 10000000 "rnd.txt")
    
 
And read the numbers in 

    (define (read-test1)
      (with-input-from-file
          "rnd.txt"
        (lambda ()
          (let lp ((i (read)))
    	(if (not (eof-object? i))
    	    (lp (read)))))))
    
scheme@(guile-user)> ,time (read-test1)
;; 37.348000s real time, 37.340000s run time.  0.450000s spent in GC.


with rdelim's read-line, it's better but still slow.

    (import (ice-9 rdelim))
    (define (read-test2)
      (with-input-from-file
          "rnd.txt"
        (lambda ()
          (let lp ((i (read-line)))
    	(if (not (eof-object? i))
    	    (begin 
    	      (string->number i)
    	      (lp (read-line))))))))
    
scheme@(guile-user)> ,time (read-test2)
;; 11.943000s real time, 11.930000s run time.  0.890000s spent in GC.


it only takes 1.8 seconds by using fscanf

    FILE *f = fopen("rnd.txt", "r");
    if (f == NULL) {
      printf("open failed!");
      exit(1);
    }
    long long i;
    while (fscanf(f, "%lld", &i) != EOF) {
      
    }

$ time ./read 

real	0m1.844s
user	0m1.803s
sys	0m0.032s

Are there any primitives in Guile that is equivalent to C's scanf?




^ permalink raw reply	[flat|nested] 9+ messages in thread
[parent not found: <mailman.97.1378137628.11215.guile-user@gnu.org>]

end of thread, other threads:[~2013-09-02 17:32 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-31  3:55 How to read integers from file faster? Darren Hoo
2013-08-31  5:46 ` Mike Gran
2013-08-31  8:59 ` Mark H Weaver
2013-08-31  9:19 ` Andy Wingo
2013-08-31 10:19   ` Ludovic Courtès
2013-09-01  8:45     ` Andy Wingo
2013-09-01 13:33       ` Ludovic Courtès
2013-09-01 20:55 ` Pascal J. Bourguignon
     [not found] <mailman.97.1378137628.11215.guile-user@gnu.org>
2013-09-02 17:32 ` Daniel Llorens

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).