measure.scm 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/bin/sh
  2. # aside from this initial boilerplate, this is actually -*- scheme -*- code
  3. main='(module-ref (resolve-module '\''(measure)) '\'main')'
  4. exec ${GUILE-guile} -l $0 -c "(apply $main (cdr (command-line)))" "$@"
  5. !#
  6. ;; A simple interpreter vs. VM performance comparison tool
  7. ;;
  8. (define-module (measure)
  9. :export (measure)
  10. :use-module (system vm vm)
  11. :use-module (system base compile)
  12. :use-module (system base language))
  13. (define (time-for-eval sexp eval)
  14. (let ((before (tms:utime (times))))
  15. (eval sexp)
  16. (let ((elapsed (- (tms:utime (times)) before)))
  17. (format #t "elapsed time: ~a~%" elapsed)
  18. elapsed)))
  19. (define *scheme* (lookup-language 'scheme))
  20. (define (measure . args)
  21. (if (< (length args) 2)
  22. (begin
  23. (format #t "Usage: measure SEXP FILE-TO-LOAD...~%")
  24. (format #t "~%")
  25. (format #t "Example: measure '(loop 23424)' lib.scm~%~%")
  26. (exit 1)))
  27. (for-each load (cdr args))
  28. (let* ((sexp (with-input-from-string (car args)
  29. (lambda ()
  30. (read))))
  31. (eval-here (lambda (sexp) (eval sexp (current-module))))
  32. (proc-name (car sexp))
  33. (proc-source (procedure-source (eval proc-name (current-module))))
  34. (% (format #t "proc: ~a~%source: ~a~%" proc-name proc-source))
  35. (time-interpreted (time-for-eval sexp eval-here))
  36. (& (if (defined? proc-name)
  37. (eval `(set! ,proc-name #f) (current-module))
  38. (format #t "unbound~%")))
  39. (the-program (compile proc-source))
  40. (time-compiled (time-for-eval `(,proc-name ,@(cdr sexp))
  41. (lambda (sexp)
  42. (eval `(begin
  43. (define ,proc-name
  44. ,the-program)
  45. ,sexp)
  46. (current-module))))))
  47. (format #t "proc: ~a => ~a~%"
  48. proc-name (eval proc-name (current-module)))
  49. (format #t "interpreted: ~a~%" time-interpreted)
  50. (format #t "compiled: ~a~%" time-compiled)
  51. (format #t "speedup: ~a~%"
  52. (exact->inexact (/ time-interpreted time-compiled)))
  53. 0))
  54. (define main measure)