- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在其他 SO 答案中找到了一些技巧,但显然我无法说服 SBCL 进行内联 fixnum 算术:
(declaim (optimize (speed 2) (safety 1)))
(declaim (ftype (function (fixnum fixnum) double-float) fixnumtest) (inline fixnumtest))
(defun fixnumtest (i j)
(declare (type fixnum i j))
(let* ((n (the fixnum (+ i j)))
(n+1 (the fixnum (1+ n))))
(declare (type fixnum n n+1))
(/ 1.0d0 (the fixnum (* n n+1)) )
)
)
(defun main ()
(format t "~11,9F~%" (fixnumtest 2 3))
)
:results in forced to do GENERIC-* (cost 30)
我还应该尝试什么?
$ sbcl --eval '(load (compile-file "play.lisp"))'
This is SBCL 1.5.1,
…
; compiling file "/opt/tmp/play.lisp" (written 16 OCT 2019 08:03:15 PM):
; compiling (DECLAIM (OPTIMIZE # ...))
; compiling (DECLAIM (FTYPE # ...) ...)
; compiling (DEFUN FIXNUMTEST ...)
; file: /opt/tmp/play.lisp
; in: DEFUN FIXNUMTEST
; (* N N+1)
;
; note: forced to do GENERIC-* (cost 30)
; unable to do inline fixnum arithmetic (cost 4) because:
; The result is a (VALUES
; (INTEGER -21267647932558653961849226946058125312
; 21267647932558653961849226946058125312)
; &OPTIONAL), not a (VALUES FIXNUM &REST T).
; unable to do inline (signed-byte 64) arithmetic (cost 5) because:
; The result is a (VALUES
; (INTEGER -21267647932558653961849226946058125312
; 21267647932558653961849226946058125312)
; &OPTIONAL), not a (VALUES (SIGNED-BYTE 64) &REST T).
; etc.
此外,我是否认为将 float 强制转换为指针(成本 13)
是从函数返回 float 的普通结果?
; (DEFUN FIXNUMTEST (I J)
; (DECLARE (TYPE FIXNUM I J))
; (LET* ((N (THE FIXNUM #)) (N+1 (THE FIXNUM #)))
; (DECLARE (TYPE FIXNUM N N+1))
; (/ 1.0d0 (THE FIXNUM (* N N+1)))))
; --> PROGN SB-IMPL::%DEFUN SB-IMPL::%DEFUN SB-INT:NAMED-LAMBDA
; ==>
; #'(SB-INT:NAMED-LAMBDA FIXNUMTEST
; (I J)
; (DECLARE (SB-C::TOP-LEVEL-FORM))
; (DECLARE (TYPE FIXNUM I J))
; (BLOCK FIXNUMTEST
; (LET* ((N #) (N+1 #))
; (DECLARE (TYPE FIXNUM N N+1))
; (/ 1.0d0 (THE FIXNUM #)))))
;
; note: doing float to pointer coercion (cost 13) to "<return value>"
最佳答案
好吧,编译器正在告诉你答案,也许是以一种有点无用的方式。如果你有两个 fixnums 那么情况就不是这样,例如,将它们相加会得到一个 fixnum:类型 fixnum
在算术运算下不闭合(甚至在 +
下也不闭合) >、-
和 *
,忽略 /
)。
来自SBCL manual :
The SBCL compiler treats type declarations differently from most other Lisp compilers. Under default compilation policy the compiler doesn’t blindly believe type declarations, but considers them assertions about the program that should be checked: all type declarations that have not been proven to always hold are asserted at runtime.
如果你想编译机器算法,你需要做的是告诉编译器它正在使用的类型足够好,它可以知道结果类型足够好,可以立即表示它们。
鉴于您在函数中的算法,并假设一个 64 位实现,那么一个好的类型是 (signed-byte 31)
:使用 (signed-byte 32)
但这失败了,因为你最终得到的东西比 (signed-byte 64)
大。
所以除了在返回时使用最后的双 float 之外,这段代码不会发出警告:
(deftype smallish-integer (&optional (bits 31))
`(signed-byte ,bits))
(declaim (ftype (function (smallish-integer smallish-integer) double-float)
fixnumtest)
(inline fixnumtest))
(defun fixnumtest (i j)
(declare (optimize (speed 2)))
(declare (type smallish-integer i j))
(let* ((n (+ i j))
(n+1 (1+ n)))
(/ 1.0d0 (* n n+1))))
值得注意的是 (signed-byte 64)
比 fixnum
大很多:这没关系,因为在一个函数中编译器可以处理适合寄存器的数字,即使它们大于 fixnums。
我对 x64 汇编器不够熟悉,无法检查所有算术是否都编译为机器指令,但看起来确实如此。
有可能说服 SBCL 编译器您不关心获得正确答案并且它应该只执行机器算术,即使它知道它可能会溢出。我不知道该怎么做。
关于optimization - 如何说服 Lisp SBCL 进行内联 fixnum 运算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58424422/
我正在尝试运行以下代码片段,以使曲线适合一些经验数据,但在Julia Optim.jl包中,optimize()方法一直存在问题。我正在使用Julia v1.1.0,并安装了所有正确的软件包。我不断收
时不时你会听到一些故事,这些故事旨在说明某人在某件事上有多擅长,有时你会听到这个人如何热衷于代码优化,以至于他优化了他的延迟循环。 因为这听起来确实是一件奇怪的事情,因为启动“计时器中断”而不是优化的
我正在尝试使用 z3py 作为优化求解器来最大化从一张纸上切出的长方体的体积。 python API 提供了 Optimize() 对象,但使用它似乎不可靠,给我的解决方案显然不准确。 我尝试使用 h
我今天接受了采访。这个问题是为了优化下面的代码。如果我们将在 for 循环之后看到下面的代码,那么下面有四个“if-else”步骤。所以,面试官要求我将其优化为 3 if-else 行。我已经尝试了很
我使用BFGS算法使用Optim.jl库来最小化Julia中的函数。今天,我问了一个关于同一个库的question,但是为了避免混淆,我决定将它分成两部分。 我还想对优化后的负逆黑森州进行估算,以进行
在 haskell 平台中实现许多功能时有一个非常常见的模式让我很困扰,但我找不到解释。这是关于使用嵌套函数进行优化。 where 子句中的嵌套函数旨在进行尾递归的原因对我来说非常清楚(如 lengt
我目前正试图利用 Julia 中的 Optim 包来最小化成本函数。成本函数是 L2 正则化逻辑回归的成本函数。其构造如下; using Optim function regularised_cost
我正在使用 GEKKO 来解决非线性规划问题。我的目标是将 GEKKO 性能与替代方案进行比较,因此我想确保我从 GEKKO 中获得其所能提供的最佳性能。 有n个二元变量,每个变量都分配有一个权
我可以手动更改参数C和epsilon以获得优化结果,但我发现有PSO(或任何其他优化算法)对SVM进行参数优化。没有算法。什么意思:PSO如何自动优化SVM参数?我读了几篇关于这个主题的论文,但我仍然
我正在使用 scipy.optimize.fmin_l_bfgs_b 来解决高斯混合问题。混合分布的均值通过回归建模,其权重必须使用 EM 算法进行优化。 sigma_sp_new, func_val
当你有一个 Option ,编译器知道 NULL永远不是 &T 的可能值, 和 encodes the None variant as NULL instead .这样可以节省空间: use std:
当你有一个 Option ,编译器知道 NULL永远不是 &T 的可能值, 和 encodes the None variant as NULL instead .这样可以节省空间: use std:
以下是说明我的问题的独立示例。 using Optim χI = 3 ψI = 0.5 ϕI(z) = z^-ψI λ = 1.0532733 V0 = 0.8522423425 zE = 0.598
根据MySQL文档关于Optimizing Queries With Explain : * ALL: A full table scan is done for each combination o
我无法预览我的 Google 优化工具体验。 Google 优化抛出以下错误: 最佳答案 我也经常遇到这种情况。 Google 给出的建议是错误的。清除 cookie 并重新启动浏览器并不能解决问题。
我一直在尝试使用 optim()或 optimize()函数来最小化绝对预测误差的总和。 我有 2 个向量,每个长度为 28,1 个包含预测数据,另一个包含过去 28 天的实际数据。 fcst和 ac
在我对各种编译器书籍和网站的独立研究中,我了解到编译器可以优化正在编译的代码的许多不同方法,但我很难弄清楚每种优化会带来多少好处给予。 大多数编译器编写者如何决定首先实现哪些优化?或者哪些优化值得付出
我在我的项目中使用 System.Web.Optimizations BundleConfig。我在我的网站上使用的特定 jQuery 插件遇到了问题。如果我将文件添加到我的 ScriptBundle
我收到这个错误 Error: webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.
scipy的optimize.fmin和optimize.leastsq有什么区别?它们似乎在 this example page 中以几乎相同的方式使用.我能看到的唯一区别是 leastsq 实际上
我是一名优秀的程序员,十分优秀!