我正在使用Antik
andGsll
进行矩阵计算。
有时我想对子网格进行算术运算(例如,将矩阵的一列乘以 2.0)。现在我必须编写这些代码:
(setf (grid:column mat 0) (gsll:elt* (grid:column mat 0) 2.0))
根据这些代码,Antik
必须先创建一个临时网格用于存储中间结果,然后将其设置为原始mat
. 当矩阵很大时,临时网格的创建可能会很慢。所以我希望我可以直接在原版上做到这一点mat
。
PS:对gsll:elt*
整个矩阵进行就地修改,例如(gsll:elt* mat 2.0)
,即使 很大,这也非常有效mat
。
更新:
我在这里展示我的实验结果:
代码:
(let ((mat (grid:make-simple-grid
:grid-type 'grid:foreign-array
:initial-contents
(loop repeat 100 collect
(loop repeat 100 collect
(random 100))))))
(time (loop repeat 1000 do
(gsll:elt* mat 2.0)
(gsll:elt* mat 0.5)))
(time (loop repeat 1000 do
(setf (grid:row mat 0) (gsll:elt* (grid:row mat 0) 2.0))
(setf (grid:row mat 0) (gsll:elt* (grid:row mat 0) 0.5)))))
结果:
Evaluation took:
0.016 seconds of real time
0.016000 seconds of total run time (0.016000 user, 0.000000 system)
100.00% CPU
46,455,124 processor cycles
353,280 bytes consed
Evaluation took:
0.446 seconds of real time
0.444000 seconds of total run time (0.420000 user, 0.024000 system)
[ Run times consist of 0.008 seconds GC time, and 0.436 seconds non-GC time. ]
99.55% CPU
1,308,042,508 processor cycles
102,275,168 bytes consed
请注意,前一种计算是在整个 100x100 矩阵上进行的,这甚至比后者更快(仅在 1x100 子矩阵上进行)。由于临时存储的分配,后者占用了更多的字节。