Re: M:SEQ Too Slow

Raymond Toy <[email protected]> 21 Jun 2002 12:09:36 -0400
Newsgroups gmane.lisp.matlisp.devel
Message-ID <[email protected]>
>>>>> "Michael" == Michael A Koerber <[email protected]> writes:

    Michael> I noted the following yesterday...

    Michael>   * (time (setf tmp (m:seq 0 32767) done 'done))
    Michael>   Evaluation took:
    Michael>     45.28 seconds of real time
    Michael>     44.78 seconds of user run time
    Michael>     0.01 seconds of system run time
    Michael>     2 page faults and
    Michael>     785416 bytes consed.
    Michael>   DONE
 
    Michael> Looking at SEQ.LISP I'd blame %PUSH-ON-END%.  Due to time constraints
    Michael> I did a quick hack to use an ARRAY and coerce it to a LIST on return.
    Michael> I DON'T KNOW IF THIS IS A GOOD FIX (the fastest and compatible with
    Michael> other MATLISP internal uses or not), but it answered the mail for me
    Michael> yesterday.  This is the new timing...

    Michael> * (time (setf tmp (seq 0 32767) done 'done))
    Michael> Evaluation took:
    Michael>   0.02 seconds of real time
    Michael>   0.02 seconds of user run time
    Michael>   0.0 seconds of system run time
    Michael>   0 page faults and
    Michael>   393224 bytes consed.
    Michael> DONE
    Michael> * 

Thanks for the note!  That is really bad.

Instead of an array, I've decided to just do (push x seq) and then
(nreverse seq) at the end.  This is quite fast for me.

I've attached my proposed solution.  It seems to be slightly faster
than your array solution and conses quite a bit less.

If you get a chance, let me know if this is better or not for you.

Ray


(defun seq (start step &optional end)
  (when (not end)
    (setq end step)
    (setq step 1))

  (let ((start (rationalize start))
	 (type (type-of step))
	 (step (rationalize step))
	 (end (rationalize end))
	 (seq nil))

    (when (zerop step)
      (error "STEP equal to 0"))
    (do ((x start (+ x step)))
	((if (> step 0)
	     (> x end)
	   (< x end))
	 (nreverse seq))
      (push (coerce x type) seq))))