Skip to content

Instantly share code, notes, and snippets.

@countvajhula
Last active July 12, 2022 01:32
Show Gist options
  • Save countvajhula/33b43c488c9e384e87420140597783a4 to your computer and use it in GitHub Desktop.
Save countvajhula/33b43c488c9e384e87420140597783a4 to your computer and use it in GitHub Desktop.
Module load time tester
#!/usr/bin/env racket
#lang cli
(require racket/port
racket/format)
#|
This works by:
1. Running `racket -l <module_name>` and `racket -l racket/base` independently
2. Subtracting the latter from the former.
3. Printing that result in milliseconds.
where <module_name> is the argument you specified at the command line,
e.g. ./loadlib.rkt racket/list
The idea is to subtract out the contribution from racket/base, so that
what remains is just the time contributed by requiring the specified module.
|#
(define (time-racket [module-name "racket/base"])
(define-values (sp out in err)
(subprocess #f #f #f (find-executable-path "time") "-p" (find-executable-path "racket") "-l" module-name))
(define result (port->string err))
(define seconds (string->number
(car
(regexp-match #px"[\\d|\\.]+"
(car
(regexp-match #rx"(?m:^real.*)"
result))))))
(close-input-port out)
(close-output-port in)
(close-input-port err)
(subprocess-wait sp)
seconds)
(program (time-require module-name)
(define base-ms (time-racket))
(define mod-ms (time-racket module-name))
(displayln (~a (* 1000 (- mod-ms base-ms)) " ms")))
(run time-require)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment