run-benchmark.chez 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. ;;; Gambit-style run-benchmark.
  2. ;;;
  3. ;;; Invoke this procedure to run a benchmark.
  4. ;;; The first argument is a string identifying the benchmark.
  5. ;;; The second argument is the number of times to run the benchmark.
  6. ;;; The third argument is a thunk that runs the benchmark.
  7. ;;; The fourth argument is a unary predicate that warns if the result
  8. ;;; returned by the benchmark is incorrect.
  9. ;;;
  10. ;;; Example:
  11. ;;; (run-benchmark "make-vector"
  12. ;;; 1
  13. ;;; (lambda () (make-vector 1000000))
  14. ;;; (lambda (v) (and (vector? v) (= (vector-length v) #e1e6))))
  15. ;;; For backward compatibility, this procedure also works with the
  16. ;;; arguments that we once used to run benchmarks in Larceny.
  17. (define (run-benchmark name arg2 . rest)
  18. (let* ((old-style (procedure? arg2))
  19. (thunk (if old-style arg2 (car rest)))
  20. (n (if old-style
  21. (if (null? rest) 1 (car rest))
  22. arg2))
  23. (ok? (if (or old-style (null? (cdr rest)))
  24. (lambda (result) #t)
  25. (cadr rest)))
  26. (result '*))
  27. (define (loop n)
  28. (cond ((zero? n) #t)
  29. ((= n 1)
  30. (set! result (thunk)))
  31. (else
  32. (thunk)
  33. (loop (- n 1)))))
  34. (if old-style
  35. (begin (newline)
  36. (display "Warning: Using old-style run-benchmark")
  37. (newline)))
  38. (newline)
  39. (display "--------------------------------------------------------")
  40. (newline)
  41. (display name)
  42. (newline)
  43. ; time is a macro supplied by Chez Scheme
  44. (time (loop n))
  45. (if (not (ok? result))
  46. (begin (display "Error: Benchmark program returned wrong result: ")
  47. (write result)
  48. (newline)))))