- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面的程序似乎非常低效。它需要多达 28.980 次 GC 时间,相比之下,非 GC 时间为 6.361 秒,SBCL 为 1.0.53。
(deftype vec3 () '(simple-array double-float (3)))
(declaim (inline make-vec3 vec3-zero
vec3-x vec3-y vec3-z
vec3-+))
(defun make-vec3 (x y z)
(declare (optimize (speed 3) (safety 0)))
(make-array 3 :element-type 'double-float
:initial-contents (list x y z)))
(defun vec3-zero ()
(make-vec3 0.0d0 0.0d0 0.0d0))
(defun vec3-x (x)
(declare (optimize (speed 3) (safety 0)))
(declare (type (simple-array double-float (3)) x))
(aref x 0))
(defun vec3-y (x)
(declare (optimize (speed 3) (safety 0)))
(declare (type (simple-array double-float (3)) x))
(aref x 1))
(defun vec3-z (x)
(declare (optimize (speed 3) (safety 0)))
(declare (type (simple-array double-float (3)) x))
(aref x 2))
(defun vec3-+ (a b)
(declare (optimize (speed 3) (safety 0)))
(make-vec3 (+ (vec3-x a) (vec3-x b))
(+ (vec3-y a) (vec3-y b))
(+ (vec3-z a) (vec3-z b))))
;; main
(defun image (x y)
(make-array (* x y) :element-type 'vec3 :initial-element (vec3-zero)))
(defun add (to from val)
(declare (type (simple-array vec3 (*)) to from)
(type vec3 val)
(optimize (speed 3) (safety 0)))
(let ((size (array-dimension to 0)))
(dotimes (i size)
(setf (aref to i) (vec3-+ (aref from i) val)))))
(defun main ()
(let ((to (image 800 800))
(x (make-vec3 1.0d0 1.0d0 1.0d0)))
(time (dotimes (i 200)
(add to to x)))
(print (aref to 0))))
* (main)
Evaluation took:
39.530 seconds of real time
35.340237 seconds of total run time (25.945526 user, 9.394711 system)
[ Run times consist of 28.980 seconds GC time, and 6.361 seconds non-GC time. ]
89.40% CPU
83,778,297,762 processor cycles
46 page faults
6,144,014,656 bytes consed
#(200.0d0 200.0d0 200.0d0)
#(200.0d0 200.0d0 200.0d0)
最佳答案
我认为在这些情况下,使用宏可能是一个好主意。接下来,我总是犹豫声明(安全 0),它会带来非常非常小的性能提升,并且如果只有 defun 内部的代码以及调用 defun 的所有代码都不是绝对正确的,则会导致奇怪的行为。
我认为这里重要的是不要在 make-vec3 中创建一个新的列表对象。我附上了你的代码的一个有点快速和肮脏的优化。在我的机器上原始代码
跑进来
; cpu time (non-gc) 27.487818 sec user, 0.008999 sec system
; cpu time (gc) 17.334368 sec user, 0.001999 sec system
; cpu time (total) 44.822186 sec user, 0.010998 sec system
; real time 44.839858 sec
; space allocation:
; 0 cons cells, 45,056,000,000 other bytes, 0 static bytes
; cpu time (non-gc) 4.075385 sec user, 0.001000 sec system
; cpu time (gc) 2.162666 sec user, 0.000000 sec system
; cpu time (total) 6.238051 sec user, 0.001000 sec system
; real time 6.240055 sec
; space allocation:
; 8 cons cells, 8,192,030,976 other bytes, 0 static bytes
(deftype vec3 () '(simple-array double-float (3)))
(declaim (optimize (speed 3) (debug 0) (safety 1)))
(defmacro make-vec3 (x y z)
`(let ((vec3
(make-array 3 :element-type 'double-float :initial-element 0.0d0)))
(setf (aref vec3 0) ,x
(aref vec3 1) ,y
(aref vec3 2) ,z)
vec3))
(defun vec3-zero ()
(make-vec3 0.0d0 0.0d0 0.0d0))
(defmacro vec3-x (x)
`(aref ,x 0))
(defmacro vec3-y (x)
`(aref ,x 1))
(defmacro vec3-z (x)
`(aref ,x 2))
(defun vec3-+ (a b)
(declare (type vec3 a b))
(make-vec3 (+ (vec3-x a) (vec3-x b))
(+ (vec3-y a) (vec3-y b))
(+ (vec3-z a) (vec3-z b))))
(defun image (x y)
(make-array (* x y) :element-type 'vec3 :initial-element (vec3-zero)))
(defun add (to from val)
(declare (type (simple-array vec3 (*)) to from)
(type vec3 val))
(let ((size (array-dimension to 0)))
(dotimes (i size)
(setf (aref to i) (vec3-+ (aref from i) val)))))
(defun main ()
(let ((to (image 800 800))
(x (make-vec3 1.0d0 1.0d0 1.0d0)))
(time (dotimes (i 200)
(add to to x)))
(print (aref to 0))))
关于performance - Common Lisp,尤其是SBCL中线性代数的高效向量运算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8356494/
为什么 (defun boolimplies (a b) (or (not a) b)) if called as(boolimplies 'a 'b) 返回 B? 即使我不使用任何 boolean
这个问题已经有答案了: Are there builtin functions for elementwise boolean operators over boolean lists? (5 个回答
我正在寻求帮助以使以下功能看起来更清晰。我觉得我可以通过使用更少的代码行来实现同样的目标。 标题看起来一定很困惑,所以让我详细说明一下。我创建了一个函数,它接受用户输入(即 72+5),将字符串拆分为
我正在学习 C++ 并尝试为矩阵编写一个 C++ 类,我在其中将矩阵存储为一维 C 数组。为此,我定义了一个 element成员函数根据矩阵元素在数组中的位置访问矩阵元素。然后我重载了 class
我正在学习 C++ 并尝试为矩阵编写一个 C++ 类,我在其中将矩阵存储为一维 C 数组。为此,我定义了一个 element成员函数根据矩阵元素在数组中的位置访问矩阵元素。然后我重载了 class
伙计们,以下内容不起作用 函数返回 true,变量返回 false,但它不会进入 when 子句。我尝试像这样放大括号 但是当我将变量的值设置为 true 并将上面的代码更改为 它进入w
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
我是原生 C 语言的新手,但我没有看到错误。 我尝试在这种情况下使用 if 操作: #define PAGE_A 0 #define PAGE_B 1 int pageID = 0; if (page
我正在从事一个项目,让用户鼠标滚轮移动并知道它向上或向下滚动。在我的代码中,我可以上下移动。但我想将 Action 保存到一个字符串中。例如,如果用户向上向上向下滚动'mhmh' 显示返回“UUD”但
我有一个 MySQL 表 payment我在其中存储客户的所有付款相关数据。表字段为:fileNo , clientName , billNo , billAmount , status 。我想构建一
我的表架构如下: +------+-------+-------+
我有这个(顺便说一句,我刚刚开始学习): #include #include using namespace std; int main() { string mystr; cout << "We
我正在用 bash 构建一个用于 Linux (SLES 11SP3) 的脚本。我想通过使用以下语法查找它的 pid 来检查某个进程是否存在: pid="$(ps -ef | grep -v grep
我有一个包含两列的表格; CREATE TABLE IF NOT EXISTS `QUESTION_CATEGORY_RELATION` ( `question_id` int(16) NOT N
我对 Python 如何计算 bool 语句感到困惑。 例如 False and 2 or 3 返回 3 这是如何评估的?我认为 Python 首先会查看“False and 2”,甚至不查看“or
这个问题在这里已经有了答案: 12 年前关闭。 这可能是非常基本的......但我似乎不明白: 如何 (2 & 1) = 0 (3 & 1) = 1 (4 & 1) = 0 等等.. 上面的这种模式似
无论如何在Haskell中定义如下函数? or True True = True or True undefined = True or True False
如您所知,TCL 有一些数学函数,例如 罪 , 因 , 和 假设 在 中调用的expr 带有 的命令() 大括号如下: puts [expr sin(1.57)] 现在如何使用 TCL 添加功能 li
让我们考虑两个数组列表。 ArrayList list1 = new ArrayList(); list1.add(1); list1.add(2); list1.add(3); ArrayList
我想包含和排除使用AND和OR的专业知识,包括与AND和OR操作正常工作。但是,当将排除专家与AND和OR一起使用时,返回与3相同的结果计数。我使用的是1.4版 Elasticsearch 。帮助我解
我是一名优秀的程序员,十分优秀!