home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / lisp / mcl / 1830 < prev    next >
Encoding:
Text File  |  1992-12-11  |  4.6 KB  |  152 lines

  1. Newsgroups: comp.lang.lisp.mcl
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!spool.mu.edu!agate!boulder!cambridge.apple.com!apple!netcomsv!netcom.com!csus.edu!wupost!darwin.sura.net!Sirius.dfn.de!mailgzrz.TU-Berlin.DE!math.fu-berlin.de!fauern!dec16!NewsWatcher!user
  3. From: poeck@informatik.uni-wuerzburg.de (karsten poeck)
  4. Subject: map-array
  5. Message-ID: <724100495.14336@news.Colorado.EDU>
  6. Followup-To: comp.lang.lisp.mcl
  7. Lines: 135
  8. Sender: news
  9. Organization: university of wuerzburg
  10. Distribution: co
  11. Date: 10 Dec 92 10:58:34 GMT
  12. Approved: news
  13. X-Note1: mail msgid was <1992Dec10.105834.20723@informatik.uni-wuerzburg.de>
  14. X-Note2: message-id generated by recnews
  15. Lines: 135
  16.  
  17. Based on the Code of Bill St.Clair for faster-climits I tried to write a
  18. general map-array function,
  19. both a mcl specific and a portable version.
  20.  
  21. Then I compared this construct to mapc for sequences
  22.  
  23. To my surprise, the mapping an array was slower than mapping over a list.
  24. Am I missing something ?
  25.  
  26. Ps I know, that       
  27. (setq min (min min wert))
  28. (setq max (max max wert))
  29. is not necessary, but I want to speed up map-array, not the operation
  30.  
  31. ;environment sys 7.01, 16Mb partition on a quadra 700 mcl 2.0p1.1
  32.  
  33. (defmacro map-array-ccl (function array1)
  34.   (let ((av (gensym))
  35.         (offset (gensym))
  36.         (asiz (gensym))
  37.         (index (gensym))
  38.         (array (gensym))
  39.         )
  40.     `(let ((,array ,array1))
  41.        (multiple-value-bind
  42.        (,av ,offset) 
  43.        (ccl::array-data-and-offset ,array)
  44.        (let ((,asiz (array-total-size ,array))
  45.              (,index ,offset))
  46.          (declare (fixnum ,asiz ,index)
  47.                   (optimize (speed 3) (safety 0)))
  48.          (if (simple-vector-p ,av)
  49.            (locally 
  50.              (declare (simple-vector ,av))
  51.              (dotimes (,(gensym) ,asiz)
  52.                (funcall ,function (aref ,av ,index))
  53.                (incf ,index)))
  54.            (dotimes (,(gensym) ,asiz)
  55.                (funcall ,function (aref ,av ,index))
  56.                (incf ,index))))))))
  57.  
  58.  
  59. (defmacro map-array-portable (function array1)
  60.   (let ((asiz (gensym))
  61.         (array (gensym))
  62.         (temparray (gensym))
  63.         (index (gensym))
  64.         )
  65.     `(let* ((,temparray ,array1)
  66.             (,asiz (array-total-size ,temparray))
  67.             (,array (make-array ,asiz
  68.                             :displaced-to ,temparray
  69.                             :element-type (array-element-type ,temparray)))
  70.             )
  71.        (declare (fixnum ,asiz)
  72.                 (optimize (speed 3) (safety 0)))
  73.        (dotimes (,index ,asiz)
  74.          (funcall ,function (aref ,array ,index))
  75.          ))))
  76.  
  77. ;test it
  78. (defparameter *long-sequence* nil)
  79.  
  80. (defparameter *array-like-sequence* nil)
  81.  
  82. (defun initialize (n)
  83.   (setq *long-sequence* nil)
  84.   (setq *array-like-sequence* (make-array (list n n) :element-type
  85. 'fixnum))
  86.   (dotimes (y n)
  87.     (dotimes (x n)
  88.       (let ((wert (random 2000)))
  89.         (push wert *long-sequence*)
  90.         (setf (aref *array-like-sequence* y x) wert))))
  91.  )
  92.  
  93. (defun test-sequence ()
  94.   (let* ((min (first *long-sequence*))
  95.          (max min))
  96.     (mapc #'(lambda(wert)
  97.               (setq min (min min wert))
  98.               (setq max (max max wert)))
  99.           *long-sequence*)
  100.     `(min ,min max ,max)))
  101.  
  102. (defun test-array-ccl ()
  103.   (let* ((min (aref *array-like-sequence* 0 0))
  104.          (max min))
  105.     (map-array-ccl #'(lambda(wert)
  106.                        (setq min (min min wert))
  107.                        (setq max (max max wert))
  108.                        )
  109.                    *array-like-sequence*)
  110.     `(min ,min max ,max))
  111.   )
  112.  
  113. (defun test-array-portable ()
  114.   (let* ((min (aref *array-like-sequence* 0 0))
  115.              (max min))
  116.         (map-array-portable #'(lambda(wert)
  117.                            (setq min (min min wert))
  118.                            (setq max (max max wert))
  119.                            )
  120.                        *array-like-sequence*)
  121.         `(min ,min max ,max)))
  122.  
  123. #|
  124. (initialize 200)
  125.  
  126. (time (test-sequence))
  127. (TEST-SEQUENCE) took 432 milliseconds (0.432 seconds) to run.
  128. Of that, 5 milliseconds (0.005 seconds) were spent in The Cooperative
  129. Multitasking Experience.
  130.  32 bytes of memory allocated.
  131. (MIN 0 MAX 1999)
  132.  
  133. (time (TEST-ARRAY-CCL))
  134.  took 502 milliseconds (0.502 seconds) to run.
  135. Of that, 7 milliseconds (0.007 seconds) were spent in The Cooperative
  136. Multitasking Experience.
  137.  32 bytes of memory allocated.
  138. (MIN 0 MAX 1999)
  139.  
  140. (time (test-array-portable))
  141. (TEST-ARRAY-PORTABLE) took 563 milliseconds (0.563 seconds) to run.
  142. Of that, 2 milliseconds (0.002 seconds) were spent in The Cooperative
  143. Multitasking Experience.
  144.  64 bytes of memory allocated.
  145. (MIN 0 MAX 1999)
  146. |#
  147.    
  148.  
  149. Karsten Poeck
  150. Universitaet  Wuerzburg
  151. Lehrstuhl fuer Informatik VI
  152.