One more optimization, getting it to 60x.

This commit is contained in:
Eli Barzilay 2010-06-20 18:32:12 -04:00
parent eec0f4bf66
commit ee57029a92

View File

@ -92,27 +92,35 @@
(bytes-set! buf n (bytes-ref lookup-byte R)) (bytes-set! buf n (bytes-ref lookup-byte R))
(loop (fx+ n 1) R)) (loop (fx+ n 1) R))
(begin (write-bytes buf out 0 (fx+ to 1)) R)))) (begin (write-bytes buf out 0 (fx+ to 1)) R))))
(define (make-line R) (define (make-line! buf start R)
(let ([buf (make-bytes (fx+ line-length 1))]) (let ([end (fx+ start line-length)])
(bytes-set! buf line-length LF) (bytes-set! buf end LF)
(let loop ([n 0] [R R]) (let loop ([n start] [R R])
(if (fx< n line-length) (if (fx< n end)
(let ([R (random-next R)]) (let ([R (random-next R)])
(bytes-set! buf n (bytes-ref lookup-byte R)) (bytes-set! buf n (bytes-ref lookup-byte R))
(loop (fx+ n 1) R)) (loop (fx+ n 1) R))
(cons buf R))))) R))))
(define LF (char->integer #\newline)) (define LF (char->integer #\newline))
(define buf (make-bytes (fx+ line-length 1))) (define buf (make-bytes (fx+ line-length 1)))
(define-values (full-lines last) (quotient/remainder N line-length)) (define-values (full-lines last) (quotient/remainder N line-length))
(define/IM (chunk r) (make-line r)) (define C
(let* ([len+1 (fx+ line-length 1)]
[buflen (fx* len+1 IM)]
[buf (make-bytes buflen)])
(let loop ([R R] [i 0])
(if (fx< i buflen)
(loop (make-line! buf i R) (fx+ i len+1))
buf))))
(bytes-set! buf line-length LF) (bytes-set! buf line-length LF)
(display header out) (display header out)
(let loop ([i full-lines] [R R]) (let loop ([i full-lines] [R R])
(cond [(fx> i 0) (let ([c (chunk R)]) (if (fx> i IM)
(write-bytes (car c)) (begin (display C out) (loop (fx- i IM) R))
(loop (fx- i 1) (cdr c)))] (let loop ([i i] [R R])
(cond [(fx> i 0) (loop (fx- i 1) (n-randoms line-length R))]
[(fx> last 0) (bytes-set! buf last LF) (n-randoms last R)] [(fx> last 0) (bytes-set! buf last LF) (n-randoms last R)]
[else R]))) [else R])))))
;; ---------------------------------------- ;; ----------------------------------------