Tuesday, September 8, 2009

Simple use of regex matching in profiling

I mentioned yesterday that one of the first things I looked at in lisp was how to concatenate strings. There are several ways and I was curious in how they compared from a system time perspective. Enter the time function. Using the sbcl implementation of time, I tried this:


(time (format nil "~a~a" "string1" "string2"))
Evaluation took:
0.000 seconds of real time
0.000000 seconds of total run time (0.000000 user, 0.000000 system)
100.00% CPU
60,264 processor cycles
3,760 bytes consed

"string1string2"


Good information, but when I am comparing many different functions which do the same thing, and compare them when using different string lengths, this printout is going to get long quick. But also notice a problem. The time function prints out some information, then returns the result of the function being timed. We don't want the result, we just want that info, as a string, to run against a regex. Hmmm. Time to look into the documentation for sbcl. Once we can get that information as a string, then we can run a regex function something like the one below, to generate an html row that we can put in a webpage table.


(defun timing-row (timing-string)
"This is to build an html table row with the output of time when used to profile a function"
(let* ((timing (cl-ppcre:all-matches-as-strings "[\.,0-9]+" timing-string))
(total-run (second timing))
(user-run (third timing))
(system-run (fifth timing))
(cycles (seventh timing))
(bytes-consed (eighth timing)))
(concatenate 'string "" bytes-consed "" cycles "" total-run ""
system-run "" user-run "")))

No comments:

Post a Comment