Re: Help with optimization
Most likely you can get it to work much quicker, but if it is only taking up
1% of the time, it wouldn't be worth it.
On 3/8/07, Young, David <deyoung@bloodhoundinc.com> wrote:
>
> Here's a simple macro/function pair from our system, used for tokenizing a
> string:
>
> (defmacro with-string-tokenizer ((str predicate) &body body)
> (let ((index (gensym))
> (data (gensym))
> (start (gensym))
> (end (gensym))
> (test-func (gensym)))
> `(let* ((,data ,str)
> (,start 0)
> (,end (length ,data))
> (,index ,start)
> (,test-func ,predicate))
> (declare (optimize (speed 3) (debug 0) (hcl:fixnum-safety 0)))
> (flet ((next-token ()
> (if (not ,index)
> nil
> (let* ((i (position-if (complement ,test-func)
> ,data :start ,index))
> (j (position-if ,test-func ,data :start i)))
> (prog1
> (subseq ,data i j)
> (setf ,index j))))))
> ,@body))))
>
> (defun tokenize (str &key delim)
> (let ((test-func (if delim (lambda (char)
> (char= char delim))
> 'lw:whitespace-char-p)))
> (with-string-tokenizer (str test-func)
> (loop for tok = (next-token)
> while tok collect tok))))
>
> It shows up during profiling (on the top of stack about 1% of the time).
> >From my point of view that's not too bad, but I'm wondering if either a)
> there's more optimizations that can be done; or b) a better way to write
> this utility. Thanks for the comments.
>
> -- david
>
> For wisdom is more precious than rubies,
> and nothing you desire can compare with her.
> -- Proverbs 8:11
>
> But all the world understands my language.
> -- Franz Joseph Haydn
>
>
> This email message is for the sole use of the intended recipients(s) and may contain confidential and privileged information of Bloodhound Software, Inc.. Any unauthorized review, use, disclosure is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
>
>
--
~jrm
Re: Help with optimization
> > ... I'm wondering if either a)
> > there's more optimizations that can be done; or b) a better way to write
> > this utility. Thanks for the comments.
> >
> > -- david
....
Concerning (b) above, with-string-tokenizer fails with trailing white space.
A fix may be:
(defmacro with-string-tokenizer ((str predicate) &body body)
(let ((index (gensym))
(data (gensym))
(start (gensym))
(end (gensym))
(test-func (gensym)))
`(let* ((,data ,str)
(,start 0)
(,end (length ,data))
(,index ,start)
(,test-func ,predicate))
(declare (optimize (speed 3) (debug 0) (hcl:fixnum-safety 0)))
(flet ((next-token ()
(if (not ,index)
nil
(let ((i (position-if (complement ,test-func)
,data :start ,index)))
(when i
(let ((j (position-if ,test-func ,data :start i)))
(prog1
(subseq ,data i j)
(setf ,index j))))))))
,@body))))
Jeff Caldwell
____________________________________________________________________________________
8:00? 8:25? 8:40? Find a flick in no time
with the Yahoo! Search movie showtime shortcut.
http://tools.search.yahoo.com/shortcuts/#news