[ prog / sol / mona ]

prog


How can I run my own instance of this

111 2020-03-03 11:34

[1/2]

The performance improvement is insignificant. The problem is clearly not the speed of calculating post ranges

insignificant
clearly

$ cat test.scm
(define *max-posts* 300)

; Splits the input string 'str into a list of strings
; based on the delimiter character 'ch
; © (Doug Hoyte, hcsw.org)
(define (string-split str ch)
  (let ((len (string-length str)))
    (letrec
      ((split
         (lambda (a b)
           (cond
             ((>= b len) (if (= a b) '() (cons (substring str a b) '())))
             ((char=? ch (string-ref str b))
              (if (= a b)
                  (split (+ 1 a) (+ 1 b))
                  (cons (substring str a b) (split b b))))
             (else (split a (+ 1 b)))))))
      (split 0 0))))

(define (flatten l)
  (letrec
    ((flat
       (lambda (l acc rest)
         (cond ((null? l)
                (if (null? rest)
                    (reverse acc)
                    (flat (car rest) acc (cdr rest))))
               ((pair? (car l))
                (flat (car l) acc (if (null? (cdr l))
                                      rest
                                      (cons (cdr l) rest))))
               (else
                 (flat (cdr l) (cons (car l) acc) rest))))))
    (flat l '() '())))

(define (posts-range range)
  (define (expand-range x)
    (cond ((> (length x) 1)
           (let* ((a (string->number (car x)))
                  (b (string->number (cadr x)))
                  (low (if (> a *max-posts*) *max-posts* a))
                  (high (if (> b *max-posts*) *max-posts* b))
                  (count (+ (- high low) 1)))
             (if (> high low) (iota count low) low)))
          (else (string->number (car x)))))
  (let* ((r1 (string-split range #\,))
         (r2 (map (lambda (x) (string-split x #\-)) r1))
         (r3 (flatten (map expand-range r2))))
    (sort (delete-duplicates r3) <)))

(define (posts-range64 range)
  (define (expand-range x)
    (cond ((> (length x) 1)
           (let* ((a (string->number (car x)))
                  (b (string->number (cadr x)))
                  (low (if (> a *max-posts*) *max-posts* a))
                  (high (if (> b *max-posts*) *max-posts* b))
                  (count (+ (- high low) 1)))
             (if (> high low)
                 (lambda () (iota count low))
                 (lambda () (list low)))))
          (else (let* ((a (string->number (car x)))
                       (low (if (> a *max-posts*) *max-posts* a)))
                  (lambda () (list low))))))
  (define (invoke-loop-set vector lamb)
    (for-each (lambda (e) (vector-set! vector e #t))
              (lamb)))
  (let* ((r1 (string-split range #\,))
         (r2 (map (lambda (x) (string-split x #\-)) r1))
         (r3 (map expand-range r2))
         (vec (make-vector (+ *max-posts* 1) #f)))
    (for-each (lambda (e) (invoke-loop-set vec e))
              r3)
    vec))

(define (timeit proc)
   (with-timings proc
      (lambda (run-time gc-time real-time)
         (write (internal-time/ticks->seconds run-time))
         (write-char #\space)
         (write (internal-time/ticks->seconds gc-time))
         (write-char #\space)
         (write (internal-time/ticks->seconds real-time))
         (newline))))

(define (stress n) (apply string-append (cons "1-300" (make-list n ",1-300"))))
301


VIP:

do not edit these