处理大约1500万行后,代码似乎只是冻结了

时间:2019-03-29 20:02:32

标签: csv io racket

在运行约2100万行的文件后,以下功能似乎只是在一段时间后停止。最多打印15520000,然后停止。我用较小的文件进行了测试,在这种情况下可以正常工作。

#lang racket

(define (read-csv name)
  (define item-hash (make-hash))
  (define user-hash (make-hash))
  (define list-hash (make-hash))
  (define item-number 0)
  (define user-number 0)
  (define n 0)

  (with-input-from-file name
    (thunk
     (for ([line (in-lines)])
       (define row (string-split line ","))
       (match-define (list user item score) row)
       (when (= item-number
                (hash-ref! item-hash item item-number))
         (set! item-number (+ 1 item-number)))
       (when (= user-number
                (hash-ref! user-hash user user-number))
         (set! user-number (+ 1 user-number)))

       (when (= 0(modulo n 10000))
         (display n)
         (display "\n"))
       (set! n (+ 1 n))

       (let ([l (hash-ref! list-hash user '())])
         (hash-set! list-hash user (cons (cons item score) l))))))

  (list item-hash user-hash list-hash))

0 个答案:

没有答案