假设您有许多列表或数组,为了示例,我们说两个:
(defparameter *arr* #(1 2 3))
(defparameter *list* '(4 5 6))
你可以使用loop
或across
关键字对它们进行in
:
(loop for elem across *arr* do (format t "~a" elem))
=> 123
(loop for elem in *list* do (format t "~a" elem))
=> 456
我希望能够使用相同的语法对这些数组或列表进行loop
。我正在使用SBCL并且执行速度是一个问题。
being the elements of
这种语法很好,因为无论它的参数是list
还是array
都可以。
(loop for elem being the elements of *arr* do (format t "~a" elem))
=> 123
(loop for elem being the elements of *list* do (format t "~a" elem))
=> 456
但它的速度是可怕的。如果我们通过访问100个元素的列表或数组1M次进行快速比较:
(format t "# Test 1.1.1 : Accessing list of doubles with loop 'in': ") (terpri)
(let ((test-list (make-list 100 :initial-element 12.2d0))
(testvar 0d0))
(declare (optimize (speed 3))
(type list test-list)
(type double-float testvar))
(time (dotimes (it 1000000 t) (loop for el in test-list do
(setf testvar (the double-float el))))))
(format t "# Test 1.1.2 : Accessing list of doubles with loop 'elements': ") (terpri)
(let ((test-list (make-list 100 :initial-element 12.2d0))
(testvar 0d0))
(declare (optimize (speed 3))
(type list test-list)
(type double-float testvar))
(time (dotimes (it 1000000 t) (loop for el being the elements of test-list do
(setf testvar (the double-float el))))))
(format t "# Test 1.2.1 : Accessing simple-array of doubles using loop 'across' : ") (terpri)
(let ((test-array (make-array 100 :initial-element 12.2d0 :element-type 'double-float))
(testvar 0d0))
(declare (optimize (speed 3))
(type double-float testvar)
(type simple-array test-array))
(time (dotimes (it 1000000 t) (loop for el across test-array do
(setf testvar (the double-float el))))))
(format t "# Test 1.2.2 : Accessing simple-array of doubles using loop 'elements' : ") (terpri)
(let ((test-array (make-array 100 :initial-element 12.2d0 :element-type 'double-float))
(testvar 0d0))
(declare (optimize (speed 3))
(type double-float testvar)
(type simple-array test-array))
(time (dotimes (it 1000000 t) (loop for el being the elements of test-array do
(setf testvar (the double-float el))))))
它给了我们:
# Test 1.1.1 : Accessing list of doubles with loop 'in':
Evaluation took:
0.124 seconds of real time
0.123487 seconds of total run time (0.123471 user, 0.000016 system)
99.19% CPU
445,008,960 processor cycles
672 bytes consed
# Test 1.1.2 : Accessing list of doubles with loop 'elements':
Evaluation took:
0.843 seconds of real time
0.841639 seconds of total run time (0.841639 user, 0.000000 system)
99.88% CPU
3,034,104,192 processor cycles
0 bytes consed
# Test 1.2.1 : Accessing simple-array of doubles using loop 'across' :
Evaluation took:
0.062 seconds of real time
0.062384 seconds of total run time (0.062384 user, 0.000000 system)
100.00% CPU
224,896,032 processor cycles
0 bytes consed
# Test 1.2.2 : Accessing simple-array of doubles using loop 'elements' :
Evaluation took:
1.555 seconds of real time
1.554472 seconds of total run time (1.541572 user, 0.012900 system)
[ Run times consist of 0.094 seconds GC time, and 1.461 seconds non-GC time. ]
99.94% CPU
5,598,161,100 processor cycles
1,600,032,848 bytes consed
我认为它必须使用elt
访问器?无论如何,速度的惩罚是不可接受的。
我写了一些东西,以实现我的目标,即为list
和array
使用相同的语法。我认为这不是很好,因为它似乎过于尴尬,但在这里:
(defun forbuild (el-sym list-or-array list-or-array-sym)
"Outputs either :
* (for el-sym in list-or-array)
* (for el-sym across list-or-array)
Depending on type of list-or-array.
el-sym : symbol, eg. 'it1
list-or-array : declared, actual data for list or array
list-or-array-sym : symbol name for the table, to avoid writing the data in full
in the 'loop' call using eval.
Example call : (forbuild 'it1 arr 'arr)"
(cond ((typep list-or-array 'array)
`(for ,el-sym across ,list-or-array-sym))
((typep list-or-array 'list)
`(for ,el-sym in ,list-or-array-sym))))
(defun forbuild-l (l-elsyms l-lars l-larsyms)
"forbuild but over lists of things."
(let ((for-list nil)
(list-temp nil))
(loop for elem in l-elsyms
for lar in l-lars
for larsym in l-larsyms do
(setf list-temp (forbuild elem lar larsym))
(loop for word-temp in list-temp do
(push word-temp for-list)))
(nreverse for-list)))
(defun loop-expr (forlist body)
"Creates the expression ready to be evaluated to execute the loop.
forlist : List of symbols to be inserted syntactically. eg.
FOR IT1 ACROSS ARR1 FOR IT2 IN ARR2
body : all the expression after the 'for' clauses in the 'loop'."
`(loop ,@forlist ,@body))
(defmacro looparl (element list-or-array &rest body)
(let ((forlist (gensym)))
`(let ((,forlist (forbuild2-l (quote ,element)
(list ,@list-or-array)
(quote ,list-or-array))))
(loop-expr ,forlist (quote ,body)))))
基本上我从参数中构建了正确的loop
语法。这里给出的looparl
版本可以这样调用:
(let ((arr1 #(7 8 9))
(list2 (list 10 11 12)))
(looparl (it1 it2) (arr1 list2) do (format t "~a ~a" it1 it2) (terpri)))
=> (LOOP FOR IT1 ACROSS ARR1
FOR IT2 IN LIST2
DO (FORMAT T "~a ~a" IT1 IT2) (TERPRI))
在此示例中省略了对此输出表达式的实际评估,因为它不适用于非全局名称。如果我们在looparl
结束时抛出一个eval:
(defmacro looparl (element list-or-array &rest body)
(let ((forlist (gensym)))
`(let ((,forlist (forbuild2-l (quote ,element)
(list ,@list-or-array)
(quote ,list-or-array))))
(eval (loop-expr ,forlist (quote ,body))))))
在全局变量上工作,我们发现我们仍然存在速度问题,因为在运行时会发生评估:
(looparl (it1 it2) (*arr* *list*) for it from 100
do (format t "~a ~a ~a" it1 it2 it) (terpri))
=> 1 4 100
2 5 101
3 6 102
(time (dotimes (iter 1000 t) (looparl (it1 it2) (*arr* *list*) for it from 100
do (format t "~a ~a ~a" it1 it2 it) (terpri))))
=> Evaluation took:
1.971 seconds of real time
1.932610 seconds of total run time (1.892329 user, 0.040281 system)
[ Run times consist of 0.097 seconds GC time, and 1.836 seconds non-GC time. ]
98.07% CPU
1,000 forms interpreted
16,000 lambdas converted
7,096,353,696 processor cycles
796,545,680 bytes consed
每次对每个宏进行一千次评估。但是在编译时肯定知道类型没有? looparl
中的语法类型非常好,我希望能够在没有速度惩罚的情况下使用它。
我在Peter Seibel's book Practical Common Lisp, chapter "Loop for Black Belts"读到了这个笔记
3你可能想知道为什么LOOP无法确定它是否在一个列表或一个向量上循环而不需要不同的介词。这是LOOP作为宏的另一个结果:列表或向量的值直到运行时才会知道,但作为宏的LOOP必须在编译时生成代码。 LOOP的设计者希望它能够生成极其高效的代码。为了能够生成有效的代码来循环,比如一个向量,它需要在编译时知道该值将是运行时的向量 - 因此,需要不同的介词。
我是在做一些大的Common-Lisp废话吗?你会如何创建一个工作,快速的looparl
?
FOR
library谢谢Ehvince参考FOR
library。 over
函数中的for:for
关键字确实正是我所需要的。然而,基准测试真的没有给人留下深刻印象:
(let ((test-list (make-list 100 :initial-element 12.2d0))
(testvar 0d0))
(declare (optimize (speed 3))
(type list test-list)
(type double-float testvar))
(time (dotimes (it 1000000 t)
(for:for ((el over test-list))
(setf testvar (the double-float el))))))
(let ((test-array (make-array 100 :initial-element 12.2d0))
(testvar 0d0))
(declare (optimize (speed 3))
(type simple-array test-array)
(type double-float testvar))
(time (dotimes (it 1000000 t)
(for:for ((el over test-array))
(setf testvar (the double-float el))))))
Evaluation took:
4.802 seconds of real time
4.794485 seconds of total run time (4.792492 user, 0.001993 system)
[ Run times consist of 0.010 seconds GC time, and 4.785 seconds non-GC time. ]
99.83% CPU
17,286,934,536 processor cycles
112,017,328 bytes consed
Evaluation took:
6.758 seconds of real time
6.747879 seconds of total run time (6.747879 user, 0.000000 system)
[ Run times consist of 0.004 seconds GC time, and 6.744 seconds non-GC time. ]
99.85% CPU
24,329,311,848 processor cycles
63,995,808 bytes consed
使用专门的关键字in
和across
的这个库的速度与标准的loop
相同。但是over
非常慢。
map
and etypecase
谢谢sds和Rainer Joswig的建议。它确实适用于我只有一个数组/列表进行迭代的简单情况。让我告诉你一个我想到的一个用例:我正在实现一个gnuplot包装器,既可以作为培训,也可以在我的工具箱中拥有自己的程序。我想从用户列表或数组中无关紧要地用作管道到gnuplot的系列。这就是为什么我需要能够同时循环多个数组/列表+使用优雅的循环子句进行迭代次数等。
在这个用例(gnuplot包装器)中,我的for
中每个“数据块”只有两个或三个loop
子句,所以我想到了根据手工输入的类型编写每个组合,这是可能的,但非常尴尬。如果我不得不做以下事情,我会陷入困境:
(loop for el1 in list1
for el2 across arr1
for el3 in list2
for el4 in list3
...)
随着list-i
和arr-i
的输入。此用例的另一个后备计划只是将所有内容转换为数组。
我认为,因为它很容易被概念化,所以我可以一劳永逸地写出一些灵活而快速的东西,但必须有一个原因,它既不是规范也不是特定于SBCL的代码。
对于琐碎的用途,你可能会这样做
(flet ((do-something (e)
...))
(etypecase foo
(vector (loop for e across foo do (do-something e)))
(list (loop for e in foo do (do-something e))))
运行时类型调度可能比使用序列抽象的通用迭代构造更快。
您正在寻找的是map
:两者
(map nil #'princ '(1 2 3))
和
(map nil #'princ #(1 2 3))
打印123
。
但是,列表和数组是非常不同的动物,最好事先决定要使用哪一个。
Shinmera的库For具有通用的over
迭代器:
(ql:quickload "for")
(for:for ((a over *arr*)
(b over *list*))
(print (list a b)))
;; (1 4)
;; (2 5)
;; (3 6)
它还具有“in”和“across”,因此在开发过程中使用“over”可能会有所帮助,如果需要,可以稍后进行优化。
我会让你做基准测试:)