shootout benchmarks

svn: r10839
This commit is contained in:
Matthew Flatt 2008-07-19 02:40:34 +00:00
parent bba44e46e8
commit b8672845fc
13 changed files with 347 additions and 350 deletions

View File

@ -0,0 +1,2 @@
#lang scheme/base
(display "hello world\n")

View File

@ -1,25 +1,20 @@
;; The Computer Language Shootout ;; The Computer Language Shootout
;; http://shootout.alioth.debian.org/ ;; http://shootout.alioth.debian.org/
(module k-nucleotide mzscheme #lang scheme/base
(require mzlib/list
mzlib/string
(only srfi/13 string-pad-right))
(define (all-counts len dna) (define (all-counts len dna)
(let ([table (make-hash-table)] (let ([table (make-hasheq)]
[seq (make-string len)]) [seq (make-string len)])
(let loop ([s (- (string-length dna) len)]) (for ([s (in-range (- (string-length dna) len) -1 -1)])
(string-copy! seq 0 dna s (+ s len)) (string-copy! seq 0 dna s (+ s len))
(let ([key (string->symbol seq)]) (let ([key (string->symbol seq)])
(let ([cnt (hash-table-get table key 0)]) (let ([cnt (hash-ref table key 0)])
(hash-table-put! table key (add1 cnt)))) (hash-set! table key (add1 cnt)))))
(unless (zero? s)
(loop (sub1 s))))
table)) table))
(define (write-freqs table) (define (write-freqs table)
(let* ([content (hash-table-map table cons)] (let* ([content (hash-map table cons)]
[total (exact->inexact (apply + (map cdr content)))]) [total (exact->inexact (apply + (map cdr content)))])
(for-each (for-each
(lambda (a) (lambda (a)
@ -29,17 +24,17 @@
(sort content (lambda (a b) (> (cdr a) (cdr b))))))) (sort content (lambda (a b) (> (cdr a) (cdr b)))))))
(define (write-one-freq table key) (define (write-one-freq table key)
(let ([cnt (hash-table-get table key 0)]) (let ([cnt (hash-ref table key 0)])
(printf "~a\t~a\n" cnt key))) (printf "~a\t~a\n" cnt key)))
(define dna (define dna
(begin (let ([in (current-input-port)])
;; Skip to ">THREE ..." ;; Skip to ">THREE ..."
(regexp-match #rx#"(?m:^>THREE.*$)" (current-input-port)) (regexp-match #rx#"(?m:^>THREE.*$)" in)
(let ([s (open-output-string)]) (let ([s (open-output-string)])
;; Copy everything but newlines to s: ;; Copy everything but newlines to s:
(let loop () (let loop ()
(when (regexp-match #rx#"\n" (current-input-port) 0 #f s) (when (regexp-match #rx#"\n" in 0 #f s)
(loop))) (loop)))
;; Extract the string from s: ;; Extract the string from s:
(string-upcase (get-output-string s))))) (string-upcase (get-output-string s)))))
@ -58,4 +53,3 @@
(string->symbol seq))) (string->symbol seq)))
'("GGT" "GGTA" "GGTATT" "GGTATTTTAATT" "GGTATTTTAATTTATAGT")) '("GGT" "GGTA" "GGTATT" "GGTATTTTAATT" "GGTATTTTAATTTATAGT"))
)

View File

@ -5,11 +5,8 @@
;; Derived from the Chicken variant, which was ;; Derived from the Chicken variant, which was
;; Contributed by Anthony Borla ;; Contributed by Anthony Borla
;; Note: as of version 350, this benchmark spends much of
;; its time GCing; it runs 2 times as fast in mzscheme3m.
;; The version that uses complex number is a little ;; The version that uses complex number is a little
;; more elegant, but slower: ;; more elegant, but much slower:
;; (define (mandelbrot iterations x y n ci) ;; (define (mandelbrot iterations x y n ci)
;; (let ((c (+ (- (/ (* 2.0 x) n) 1.5) ;; (let ((c (+ (- (/ (* 2.0 x) n) 1.5)
;; (* ci 0.0+1.0i)))) ;; (* ci 0.0+1.0i))))
@ -19,10 +16,8 @@
;; [(> (magnitude z) 2.0) 0] ;; [(> (magnitude z) 2.0) 0]
;; [else (loop (add1 i) (+ (* z z) c))])))) ;; [else (loop (add1 i) (+ (* z z) c))]))))
#lang scheme/base
(module mandelbrot mzscheme (require scheme/cmdline)
;; -------------------------------
(define +limit-sqr+ 4.0) (define +limit-sqr+ 4.0)
@ -39,13 +34,14 @@
(ziq (* zi zi))) (ziq (* zi zi)))
(cond (cond
((> (+ zrq ziq) +limit-sqr+) 0) ((> (+ zrq ziq) +limit-sqr+) 0)
(else (loop (add1 i) (+ (- zrq ziq) cr) (+ (* 2.0 (* zr zi)) ci))))))))) (else (loop (add1 i)
(+ (- zrq ziq) cr)
(+ (* 2.0 (* zr zi)) ci)))))))))
;; ------------------------------- ;; -------------------------------
(define (main args) (define (main n)
(let ((n (string->number (vector-ref args 0))) (let ((out (current-output-port)))
(out (current-output-port)))
(fprintf out "P4\n~a ~a\n" n n) (fprintf out "P4\n~a ~a\n" n n)
@ -71,10 +67,13 @@
(begin (begin
(when (positive? bitnum) (when (positive? bitnum)
(write-byte (arithmetic-shift byteacc (- 8 (bitwise-and n #x7))) out)) (write-byte (arithmetic-shift byteacc
(- 8 (bitwise-and n #x7)))
out))
(loop-y (add1 y)))))))))) (loop-y (add1 y))))))))))
;; ------------------------------- ;; -------------------------------
(main (current-command-line-arguments))) (command-line #:args (n)
(main (string->number n)))

View File

@ -146,8 +146,7 @@ Correct output N = 1000 is
(printf "~a~%" (real->decimal-string (energy system) 9)) (printf "~a~%" (real->decimal-string (energy system) 9))
(do ((i 1 (+ i 1))) (for ([i (in-range 0 n)])
((< n i))
(advance system 0.01)) (advance system 0.01))
(printf "~a~%" (real->decimal-string (energy system) 9))) (printf "~a~%" (real->decimal-string (energy system) 9)))

View File

@ -7,44 +7,37 @@
;; Written by Dima Dorfman, 2004 ;; Written by Dima Dorfman, 2004
;; Converted to MzScheme by Brent Fulgham ;; Converted to MzScheme by Brent Fulgham
(module nsieve mzscheme #lang scheme/base
(require (only srfi/13 string-index string-pad)) (require scheme/cmdline)
(define (nsieve m) (define (nsieve m)
(let ((a (make-vector m #t))) (let ((a (make-vector m #t)))
(let loop ((i 2) (n 0)) (let loop ((i 2) (n 0))
(if (< i m) (if (< i m)
(begin
(if (vector-ref a i) (if (vector-ref a i)
(begin (begin
(let clear ((j (+ i i))) (let clear ((j (+ i i)))
(if (< j m) (when (< j m)
(begin
(vector-set! a j #f) (vector-set! a j #f)
(clear (+ j i))))) (clear (+ j i))))
(loop (+ 1 i) (+ 1 n))) (loop (+ 1 i) (+ 1 n)))
(loop (+ 1 i) n))) (loop (+ 1 i) n))
n)))) n))))
(define (string-pad s len)
(string-append (make-string (- len (string-length s)) #\space)
s))
(define (test n) (define (test n)
(let* ((m (* (expt 2 n) 10000)) (let* ((m (* (expt 2 n) 10000))
(count (nsieve m))) (count (nsieve m)))
(printf "Primes up to ~a ~a~%" (printf "Primes up to ~a ~a\n"
(string-pad (number->string m) 8) (string-pad (number->string m) 8)
(string-pad (number->string count) 8)))) (string-pad (number->string count) 8))))
(define (main args) (define (main n)
(if (< (vector-length args) 1) (when (>= n 0) (test n))
(begin (when (>= n 1) (test (- n 1)))
(display "An argument is required") (newline) 2) (when (>= n 2) (test (- n 2))))
(let ((n (string->number (vector-ref args 0))))
(if (not n)
(begin
(display "An integer is required") (newline) 2)
(begin
(if (>= n 0) (test n))
(if (>= n 1) (test (- n 1)))
(if (>= n 2) (test (- n 2)))
0)))))
(main (current-command-line-arguments))) (command-line #:args (n) (main (string->number n)))

View File

@ -4,9 +4,9 @@
;; ;;
;; Adapted from CMUCL code by Dima Dorfman; bit-vector stuff by Alex Shinn; ;; Adapted from CMUCL code by Dima Dorfman; bit-vector stuff by Alex Shinn;
;; cobbled together by felix, converted to MzScheme by Brent Fulgham ;; cobbled together by felix, converted to MzScheme by Brent Fulgham
;; Note: Requires MzScheme 299+
(module nsievebits mzscheme #lang scheme/base
(require scheme/cmdline)
(define (make-bit-vector size) (define (make-bit-vector size)
(let* ((len (quotient (+ size 7) 8)) (let* ((len (quotient (+ size 7) 8))
@ -47,20 +47,20 @@
(clear i) (clear i)
(set! c (add1 c))))))) (set! c (add1 c)))))))
(define (string-pad s n) (define (string-pad s len)
(string-append (make-string (- n (string-length s)) #\space) (string-append (make-string (- len (string-length s)) #\space)
s)) s))
(define (test n) (define (test n)
(let ((m (* 10000 (arithmetic-shift 1 n)))) (let* ((m (* (expt 2 n) 10000))
(printf "Primes up to ~a ~a~%" (count (nsievebits m)))
(printf "Primes up to ~a ~a\n"
(string-pad (number->string m) 8) (string-pad (number->string m) 8)
(string-pad (number->string (nsievebits m)) 8)))) (string-pad (number->string count) 8))))
(define (main args) (define (main n)
(let ([n (string->number (vector-ref args 0))])
(when (>= n 0) (test n)) (when (>= n 0) (test n))
(when (>= n 1) (test (- n 1))) (when (>= n 1) (test (- n 1)))
(when (>= n 2) (test (- n 2))))) (when (>= n 2) (test (- n 2))))
(main (current-command-line-arguments))) (command-line #:args (n) (main (string->number n)))

View File

@ -8,23 +8,17 @@
;; Contributed by Anthony Borla ;; Contributed by Anthony Borla
;; --------------------------------------------------------------------- ;; ---------------------------------------------------------------------
;; Note: as of version 350, this benchmark spends much of #lang scheme/base
;; its time GCing; it runs 1.5 times as fast in mzscheme3m. (require scheme/cmdline)
(module partialsums mzscheme
(require (only mzlib/string real->decimal-string))
(let ((n (exact->inexact (let ((n (exact->inexact
(string->number (string->number
(vector-ref (current-command-line-arguments) 0)))) (command-line #:args (n) n)))))
(alt 1) (d2 0) (d3 0) (ds 0) (dc 0)
(s0 0) (s1 0) (s2 0) (s3 0) (s4 0) (s5 0) (s6 0) (s7 0) (s8 0))
(let loop ([d 0.0] (let loop ([d 0.0]
(alt 1) (d2 0) (d3 0) (ds 0) (dc 0) (alt 1) (d2 0) (d3 0) (ds 0) (dc 0)
(s0 0) (s1 0) (s2 0) (s3 0) (s4 0) (s5 0) (s6 0) (s7 0) (s8 0)) (s0 0) (s1 0) (s2 0) (s3 0) (s4 0) (s5 0) (s6 0) (s7 0) (s8 0))
(if (= d n #;(+ n 1)) (if (= d n)
(let ([format-result (let ([format-result
(lambda (str n) (lambda (str n)
(printf str (real->decimal-string n 9)))]) (printf str (real->decimal-string n 9)))])
@ -58,4 +52,4 @@
(loop d (loop d
alt d2 d3 ds dc alt d2 d3 ds dc
s0 s1 s2 s3 s4 s5 s6 s7 s8)))))) s0 s1 s2 s3 s4 s5 s6 s7 s8)))))

View File

@ -8,8 +8,8 @@
;; Contributed by Anthony Borla ;; Contributed by Anthony Borla
;; --------------------------------------------------------------------- ;; ---------------------------------------------------------------------
(module recursive mzscheme #lang scheme/base
(require (only mzlib/string real->decimal-string)) (require scheme/cmdline)
;; ------------------------------- ;; -------------------------------
@ -40,8 +40,7 @@
;; ------------------------------- ;; -------------------------------
(define (main args) (define (main n)
(let ((n (string->number (vector-ref args 0))))
(printf "Ack(3,~A): ~A~%" n (ack 3 n)) (printf "Ack(3,~A): ~A~%" n (ack 3 n))
(printf "Fib(~a): ~a~%" (printf "Fib(~a): ~a~%"
@ -52,8 +51,8 @@
(printf "Tak(~A,~A,~A): ~A~%" (* n 3) (* n 2) n (tak (* n 3) (* n 2) n)) (printf "Tak(~A,~A,~A): ~A~%" (* n 3) (* n 2) n (tak (* n 3) (* n 2) n))
(printf "Fib(3): ~A~%" (fib 3)) (printf "Fib(3): ~A~%" (fib 3))
(printf "Tak(3.0,2.0,1.0): ~a~%" (real->decimal-string (takflt 3.0 2.0 1.0) 1)))) (printf "Tak(3.0,2.0,1.0): ~a~%" (real->decimal-string (takflt 3.0 2.0 1.0) 1)))
;; ------------------------------- ;; -------------------------------
(main (current-command-line-arguments))) (main (command-line #:args (n) (string->number n)))

View File

@ -10,9 +10,8 @@
;; Contributed by Anthony Borla ;; Contributed by Anthony Borla
;; --------------------------------------------------------------------- ;; ---------------------------------------------------------------------
(module regexpdna mzscheme #lang scheme/base
(require scheme/port)
(require mzlib/port)
;; ------------------------------- ;; -------------------------------
@ -86,4 +85,4 @@
(printf "~%~A~%~A~%~A~%" (printf "~%~A~%~A~%~A~%"
(bytes-length orig) (bytes-length orig)
(bytes-length filtered) (bytes-length filtered)
(bytes-length replaced))))) (bytes-length replaced))))

View File

@ -27,7 +27,7 @@
;; return element i,j of infinite matrix A ;; return element i,j of infinite matrix A
(define (A i j) (define (A i j)
(/ 1.0 (+ (* (+ i j) (/ (+ i j 1) 2)) i 1))) (/ 1.0 (+ (* (+ i j) (/ (+ i (+ j 1)) 2)) (+ i 1))))
;; multiply vector v by matrix A ;; multiply vector v by matrix A
(define (MultiplyAv n v Av) (define (MultiplyAv n v Av)

View File

@ -1,10 +1,5 @@
;;; http://shootout.alioth.debian.org/ #lang scheme/base
;;;
;;; Contributed by Eli Barzilay
(module sumcol mzscheme (for/fold ([acc 0])
(let loop ([acc 0]) ([n (in-lines)])
(let ([n (read)]) (+ acc (string->number n)))
(if (eof-object? n)
(printf "~a\n" acc)
(loop (+ acc n))))))

View File

@ -0,0 +1,23 @@
#lang scheme/base
(require scheme/cmdline)
;; Each thread runs this loop:
(define (run id next)
(let ([v (thread-receive)])
(cond
[(zero? v) ;; Done
(printf "~a\n" id)
(exit)]
[else ;; Keep going
(thread-send next (sub1 v))
(run id next)])))
(let ([n (command-line #:args (n) (string->number n))])
;; The original thread is #503. Create the rest:
(let ([t1 (for/fold ([next (current-thread)])
([id (in-range 502 0 -1)])
(thread (lambda () (run id next))))])
;; Start:
(thread-send t1 n)
(run 503 t1)))