CL and iterators - a newbie question

sirola_at_fisica.unige.it
Date: 05/23/04


Date: Sun, 23 May 2004 18:36:51 GMT

Hello,
I'm learning lisp and, as an exercise, trying to implement iterators
with CL. I implemented a simple as follows, using a closure:

(defun sarray-iterator (v &key (start 0))
  (declare (type (simple-array double-float) v)
           (type fixnum start))
  (let ((next-position start))
    (declare (type fixnum next-position)
             (type double-float result))
    #'(lambda ()
        (let ((result 0d0))
          (setq result (aref v next-position))
          (incf next-position)
          result))))

I tried to profile it a bit using the following code:

(setf (symbol-function 'try2) (sarray-iterator
                               (make-array 500000
                                           :element-type 'double-float
                                           :initial-element 1.0d0)
                               :start 0))
(profile:profile try2)
(try2)
(profile:report-time)

the profiler (cmucl) reported

  Consed | Calls | Secs | Sec/Call | Bytes/C. | Name:
-----------------------------------------------------------------------
         16 | 1 | 0.000 | 0.00000 | 16 | TRY2

so it seems the iterator conses 16 bytes per call (2 double
floats?), am I right? Where does the cons happen? Is it possible to
write a non-consing iterator?
Thanks in advance,
e.

-- 
Enrico Sirola


Relevant Pages

  • Re: Need help with Vector parameter when invoking C dll from C#
    ... I have the structure CUserContextData declare correctly in C# already. ... What you could do is implement a function in a C++ wrapper that takes an array of type CUserContextData and convert this into a vector of type CUserContextData, then you need to declare a vector iterator for this vector and pass the iterator to the EncodeAsnUser function. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Help with a Remote Type / Iterator
    ... we'd rather declare all of the types in the spec and ... We've tried a passive iterator, but some of the stuff we ... > package must be a remote subprogram. ... particular partition, then do the next partition, iterate over all ...
    (comp.lang.ada)
  • Re: CL and iterators - a newbie question
    ... If you compile your code with (DECLARE (OPTIMIZE SPEED)), ... returned from a non-local function, ... Moving from non-descriptor to descriptor requires ... If you create iterator and iterate in the same function, ...
    (comp.lang.lisp)
  • Re: trying to declare an iterator for a std::vector of template pointers
    ... > int main ... > Can anyone tell me if/how to declare an iterator to a vector of template ...
    (comp.lang.cpp)