gpt4 book ai didi

lisp - 优化选项 sbcl

转载 作者:太空宇宙 更新时间:2023-11-03 18:59:58 24 4
gpt4 key购买 nike

如何为所有功能设置优化选项,如“speed 3”?

(declaim (optimize (speed 3) (debug 0) (safety 0))

没用(

最佳答案

%> cat test.lisp
(defun fib (n &optional (a 0) (b 1))
(declare (type fixnum n))
(if (zerop n)
a
(fib (1- n) b (+ a b))))


%> sbcl
* (compile-file "test.lisp")

; compiling file "/pussycat/natty-home/westerp/test.lisp" (written 24 MAR 2017 02:40:48 PM):
; compiling (DEFUN FIB ...)

; /pussycat/natty-home/westerp/test.fasl written
; compilation finished in 0:00:00.048
#P"/pussycat/natty-home/westerp/test.fasl"
NIL
NIL
* (load "test.fasl")

T

让我们检查反汇编一个函数的输出:

* (disassemble 'fib)

; disassembly for FIB
; Size: 148 bytes. Origin: #x1003A5529E (segment 1 of 3)
; 29E: L0: 498B4C2460 MOV RCX, [R12+96] ; thread.binding-stack-pointer
; no-arg-parsing entry point
; 2A3: 48894DF8 MOV [RBP-8], RCX
; 2A7: 4D85D2 TEST R10, R10
; 2AA: 7509 JNE L1
; 2AC: 498BD1 MOV RDX, R9
; 2AF: 488BE5 MOV RSP, RBP
; 2B2: F8 CLC
; 2B3: 5D POP RBP
; 2B4: C3 RET
; 2B5: L1: 498BD2 MOV RDX, R10
; 2B8: 48D1FA SAR RDX, 1
; 2BB: 488BDA MOV RBX, RDX
; 2BE: 4883EB01 SUB RBX, 1
; 2C2: 48895DE8 MOV [RBP-24], RBX
; 2C6: 4C8945F0 MOV [RBP-16], R8
; 2CA: 4C894DE0 MOV [RBP-32], R9
; 2CE: 4C8955D8 MOV [RBP-40], R10
; 2D2: 498BD1 MOV RDX, R9
; 2D5: 498BF8 MOV RDI, R8
; 2D8: 41BBC0010020 MOV R11D, 536871360 ; GENERIC-+
; 2DE: 41FFD3 CALL R11
; 2E1: 488BF2 MOV RSI, RDX
; 2E4: 4C8B55D8 MOV R10, [RBP-40]
; 2E8: 4C8B4DE0 MOV R9, [RBP-32]
; 2EC: 488B5DE8 MOV RBX, [RBP-24]
; 2F0: 4C8B45F0 MOV R8, [RBP-16]
; 2F4: 488BD3 MOV RDX, RBX
; 2F7: 48D1E2 SHL RDX, 1
; 2FA: 710C JNO L2
; 2FC: 488BD3 MOV RDX, RBX
; 2FF: 41BB70060020 MOV R11D, 536872560 ; ALLOC-SIGNED-BIGNUM-IN-RDX
; 305: 41FFD3 CALL R11
; 308: L2: 498BF8 MOV RDI, R8
; 30B: 488B05DEFEFFFF MOV RAX, [RIP-290] ; #<FDEFINITION for FIB>
; 312: B906000000 MOV ECX, 6
; 317: FF7508 PUSH QWORD PTR [RBP+8]
; 31A: FF6009 JMP QWORD PTR [RAX+9]
; Origin #x1003A5531D (segment 2 of 3)
; 31D: 31C9 XOR ECX, ECX ; :OPTIONAL entry point
; Origin #x1003A5531F (segment 3 of 3)
; 31F: 4C8BD2 MOV R10, RDX ; :OPTIONAL entry point
; 322: 4C8BC9 MOV R9, RCX
; 325: 41B802000000 MOV R8D, 2
; 32B: E96EFFFFFF JMP L0
; 330: CC10 BREAK 16 ; Invalid argument count trap
NIL

让我们改变它的速度:

%> cat test2.lisp
(declaim (optimize (speed 3) (debug 0) (safety 0))

(defun fib (n &optional (a 0) (b 1))
(declare (type fixnum n))
(if (zerop n)
a
(fib (1- n) b (+ a b))))

%> sbcl
* (compile-file "test2.lisp")

; compiling file "/pussycat/natty-home/westerp/test2.lisp" (written 24 MAR 2017 02:45:55 PM):
; compiling (DECLAIM (OPTIMIZE # ...))
; compiling (DEFUN FIB ...)
; file: /pussycat/natty-home/westerp/test2.lisp
; in: DEFUN FIB
; (+ A B)
;
; note: forced to do GENERIC-+ (cost 10)
; unable to do inline float arithmetic (cost 2) because:
; The first argument is a NUMBER, not a DOUBLE-FLOAT.
; The second argument is a NUMBER, not a DOUBLE-FLOAT.
; The result is a (VALUES NUMBER &OPTIONAL), not a (VALUES DOUBLE-FLOAT
; &REST T).
; unable to do inline float arithmetic (cost 2) because:
; The first argument is a NUMBER, not a SINGLE-FLOAT.
; The second argument is a NUMBER, not a SINGLE-FLOAT.
; The result is a (VALUES NUMBER &OPTIONAL), not a (VALUES SINGLE-FLOAT
; &REST T).
; etc.
;
; compilation unit finished
; printed 1 note


; /pussycat/natty-home/westerp/test2.fasl written
; compilation finished in 0:00:00.012
#P"/pussycat/natty-home/westerp/test2.fasl"
NIL
NIL
* (load "test2.fasl")

T

让我们检查反汇编一个函数的输出:

* (disassemble 'fib)

; disassembly for FIB
; Size: 79 bytes. Origin: #x1003A44CB0 (segment 1 of 3)
; B0: L0: 488D0C00 LEA RCX, [RAX+RAX] ; no-arg-parsing entry point
; B4: 4885C9 TEST RCX, RCX
; B7: 7509 JNE L1
; B9: 488BD3 MOV RDX, RBX
; BC: 488BE5 MOV RSP, RBP
; BF: F8 CLC
; C0: 5D POP RBP
; C1: C3 RET
; C2: L1: 488975F0 MOV [RBP-16], RSI
; C6: 4C8BC0 MOV R8, RAX
; C9: 4983E801 SUB R8, 1
; CD: 4C8945F8 MOV [RBP-8], R8
; D1: 488BD3 MOV RDX, RBX
; D4: 488BFE MOV RDI, RSI
; D7: 41BBC0010020 MOV R11D, 536871360 ; GENERIC-+
; DD: 41FFD3 CALL R11
; E0: 488BCA MOV RCX, RDX
; E3: 488B75F0 MOV RSI, [RBP-16]
; E7: 4C8B45F8 MOV R8, [RBP-8]
; EB: 498BC0 MOV RAX, R8
; EE: 488BDE MOV RBX, RSI
; F1: 488BF1 MOV RSI, RCX
; F4: EBBA JMP L0
; Origin #x1003A44CF6 (segment 2 of 3)
; F6: 31DB XOR EBX, EBX ; :OPTIONAL entry point
; Origin #x1003A44CF8 (segment 3 of 3)
; F8: BE02000000 MOV ESI, 2 ; :OPTIONAL entry point
; FD: EBB1 JMP L0
NIL

优化后的编译函数长度为 79 个字节,而标准设置为 148 个字节。如果你添加更多的调试和安全,我得到 229 字节长。明显有区别。

关于lisp - 优化选项 sbcl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42940068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com