;;AUTOLOAD: time
;;AUTODOC: time "profiles the time spent in an expression"
;;(time expression [time-type]) 
;;
;;Returns the real time in milliseconds needed to eval the expression, that is
;;quoted by the function
;;(time (fib 20)) is used as a performance test in klone, for instance
;;time-type is an optional parameter that can be 
;; :real (default) to get real elapsed time
;; :user The CPU time
;; :sys The CPU time used the kernel calls
;; :cpu The total CPU time used. useful for benchmarking, is the sum 
;;      of user and system times.


(defunq time (time:l &optional (type :real))
  (with (time:time (get-internal-run-time :type type))
    (eval time:l)
    (- (get-internal-run-time :type type) time:time)
))

;;AUTOLOAD: timen
;;AUTODOC: timen "profiles the time spent in an expression, repeated"
;;(timen N expression)
;;
;; Returns the real time in milliseconds needed to eval
;;the expression, executed N times for better precision
;;(time 1000 (get p-list 'foo)) and (timen 1000 (get table 'foo))
;;can be used for instance to measure access times

(defmacro timen (time:N &rest time:l)
  `(with (
      time:time (get-internal-run-time)
      time:dummy (dotimes ,time:N ,@time:l)
      time:newtime (get-internal-run-time)
    )
    (print-format () "%0 microseconds per iteration\n"
      (/ (* (- time:newtime time:time) 1000) ,time:N)
    )
    (- (get-internal-run-time) time:time)
))
