Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defstruct state
- (samples 0)
- real-time
- (real-time/overhead 0.0)
- (real-time/total 0)
- gc-time
- (gc-time/total 0.0)
- bytes-consed
- (bytes-consed/total 0))
- (defstruct (results (:constructor %make-results))
- samples
- real-time
- gc-time
- bytes-consed)
- (defun get-time ()
- #-sbcl (multiple-value-bind (s us) (sb-ext:get-time-of-day)
- (+ (* (expt 10 6)
- (- s (load-time-value (sb-ext:get-time-of-day))))
- us))
- #+sbcl (* (expt 10 6)
- (- (/ (get-internal-real-time)
- internal-time-units-per-second)
- (load-time-value (/ (get-internal-real-time)
- internal-time-units-per-second)))))
- (defun reset-state (state)
- (setf (state-real-time state) (get-time)
- (state-gc-time state) sb-ext:*gc-run-time*
- (state-bytes-consed state) (sb-ext:get-bytes-consed)))
- (defun accumulate-state (state)
- (let ((real-time (get-time)))
- (incf (state-samples state))
- (incf (state-real-time/total state)
- (- (get-time) (state-real-time state)))
- (incf (state-gc-time/total state)
- (- sb-ext:*gc-run-time* (state-gc-time state)))
- (incf (state-bytes-consed/total state)
- (- (sb-ext:get-bytes-consed) (state-bytes-consed state)))
- (incf (state-real-time/overhead state)
- (max 0 (- (get-time) real-time)))))
- (defun make-results (state)
- (%make-results
- :samples (state-samples state)
- :real-time (/ (- (state-real-time/total state)
- (state-real-time/overhead state))
- (expt 10 6))
- :gc-time (/ (state-gc-time/total state)
- internal-time-units-per-second)
- :bytes-consed (state-bytes-consed/total state)))
- (defmacro with-benchmark (samples &body body)
- (au:with-unique-names (state)
- `(let ((,state (make-state)))
- (loop :repeat ,samples
- :do (reset-state ,state)
- (progn ,@body)
- (accumulate-state ,state))
- (make-results ,state))))
- (defun test ()
- (time (loop :repeat (expt 10 7) :do (list 1 2 3)))
- (with-benchmark (expt 10 7) (list 1 2 3)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement