timing.scm 797 B

12345678910111213141516171819202122232425262728
  1. (library (timing)
  2. (export time)
  3. (import
  4. (except (rnrs base)
  5. let-values
  6. map
  7. error
  8. vector-map)
  9. (only (guile)
  10. lambda* λ
  11. simple-format)
  12. ;; format
  13. (ice-9 format)
  14. ;; time stuff
  15. (srfi srfi-19))
  16. (define-syntax time
  17. (syntax-rules ()
  18. [(time expr expr* ...)
  19. (let ([start-time (current-time time-monotonic)])
  20. (let ([result (begin expr expr* ...)])
  21. (let ([end-time (current-time time-monotonic)])
  22. (let* ([diff (time-difference end-time start-time)]
  23. [elapsed-ns (+ (/ (time-nanosecond diff) 1e9)
  24. (time-second diff))])
  25. (simple-format #t "~a\n" (format #f "~fs~%" elapsed-ns)))
  26. result)))])))