;;;; Programmer: Kevin M. Rosenberg
;;;; Date Started: Apr 2000
;;;;
-;;;; $Id: genutils.lisp,v 1.13 2002/12/14 18:51:53 kevin Exp $
+;;;; $Id: genutils.lisp,v 1.14 2003/01/13 21:40:20 kevin Exp $
;;;;
;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
;;;;
year
hr min sec))))
+
+;; Benchmarking
+
+(defun print-float-units (val unit)
+ (cond
+ ((< val 1d-6)
+ (format t "~,2,9F nano~A" val unit))
+ ((< val 1d-3)
+ (format t "~,2,6F micro~A" val unit))
+ ((< val 1)
+ (format t "~,2,3F milli~A" val unit))
+ ((> val 1d9)
+ (format t "~,2,-9F giga~A" val unit))
+ ((> val 1d6)
+ (format t "~,2,-6F mega~A" val unit))
+ ((> val 1d3)
+ (format t "~,2,-3F kilo~A" val unit))
+ (t
+ (format t "~,2F ~A" val unit))))
+
+(defun print-seconds (secs)
+ (print-float-units secs "sec"))
+
+(defmacro time-iterations (n &body body)
+ (let ((i (gensym))
+ (count (gensym)))
+ `(progn
+ (let ((,count ,n))
+ (format t "~&Test with ~d iterations: ~W" ,count (quote ,body))
+ (let ((t1 (get-internal-real-time)))
+ (dotimes (,i ,count)
+ ,@body)
+ (let* ((t2 (get-internal-real-time))
+ (secs (coerce (/ (- t2 t1)
+ internal-time-units-per-second)
+ 'double-float)))
+ (format t "~&Total time: ")
+ (print-seconds secs)
+ (format t ", time per iteration: ")
+ (print-seconds (coerce (/ secs ,n) 'double-float))))))))
+
;;;; Programmer: Kevin M. Rosenberg
;;;; Date Started: Apr 2000
;;;;
-;;;; $Id: package.lisp,v 1.18 2002/12/26 11:57:07 kevin Exp $
+;;;; $Id: package.lisp,v 1.19 2003/01/13 21:40:20 kevin Exp $
;;;;
;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
;;;;
#:in
#:mean
#:with-gensyms
-
+ #:time-iterations
+ #:print-float-units
+ #:print-seconds
+
;; strings.lisp
#:string-append
#:count-string-words
#:string-invert
#:escape-xml-string
#:string-replace-char-string
-
+ #:make-usb8-array
+ #:usb8-array-to-string
+ #:string-to-usb8-array
+
;; symbols.lisp
#:ensure-keyword
#:concat-symbol
;;;; Programmer: Kevin M. Rosenberg
;;;; Date Started: Apr 2000
;;;;
-;;;; $Id: strings.lisp,v 1.5 2002/12/28 07:59:37 kevin Exp $
+;;;; $Id: strings.lisp,v 1.6 2003/01/13 21:40:20 kevin Exp $
;;;;
;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
;;;;
new-string)))))
+
+(defun make-usb8-array (len)
+ (make-array len :adjustable nil
+ :fill-pointer nil
+ :element-type '(unsigned-byte 8)))
+
+(defun usb8-array-to-string (vec)
+ (let* ((len (length vec))
+ (str (make-string len)))
+ (declare (fixnum len)
+ (simple-string str)
+ (optimize (speed 3)))
+ (dotimes (i len)
+ (declare (fixnum i))
+ (setf (schar str i) (code-char (aref vec i))))
+ str))
+
+(defun string-to-usb8-array (str)
+ (let* ((len (length str))
+ (vec (make-usb8-array len)))
+ (declare (fixnum len)
+ (type (array fixnum (*)) vec)
+ (optimize (speed 3)))
+ (dotimes (i len)
+ (declare (fixnum i))
+ (setf (aref vec i) (char-code (schar str i))))
+ vec))
+