gpt4 book ai didi

assembly - 比较 Common Lisp 中的 List/Make-list 和 Vector/Make-array

转载 作者:行者123 更新时间:2023-12-01 11:13:16 25 4
gpt4 key购买 nike

listmake-list 的汇编代码有些不同(在 SBCL 中),即使最终结果相同:

* (disassemble (lambda (x) (list x)))
; disassembly for (LAMBDA (X))
; Size: 77 bytes. Origin: #x10025C0064
; 64: 498B4560 MOV RAX, [R13+96] ; no-arg-parsing entry point
; thread.binding-stack-pointer
; 68: 488945F8 MOV [RBP-8], RAX
; 6C: 840425F8FF1020 TEST AL, [#x2010FFF8] ; safepoint
; 73: 4D8B5D20 MOV R11, [R13+32] ; thread.alloc-region
; 77: 498D4310 LEA RAX, [R11+16]
; 7B: 493B4528 CMP RAX, [R13+40]
; 7F: 7725 JNBE L1
; 81: 49894520 MOV [R13+32], RAX ; thread.alloc-region
; 85: L0: 498D4307 LEA RAX, [R11+7]
; 89: 840425F8FF1020 TEST AL, [#x2010FFF8] ; safepoint
; 90: 488950F9 MOV [RAX-7], RDX
; 94: C7400117001120 MOV DWORD PTR [RAX+1], #x20110017 ; NIL
; 9B: 488BD0 MOV RDX, RAX
; 9E: 488BE5 MOV RSP, RBP
; A1: F8 CLC
; A2: 5D POP RBP
; A3: C3 RET
; A4: CC0F BREAK 15 ; Invalid argument count trap
; A6: L1: 6A10 PUSH 16
; A8: FF142528000020 CALL QWORD PTR [#x20000028] ; ALLOC-TRAMP-R11
; AF: EBD4 JMP L0
NIL

* (disassemble (lambda (x) (make-list 1 :initial-element x)))
; disassembly for (LAMBDA (X))
; Size: 43 bytes. Origin: #x10025C0127
; 27: 498B5D60 MOV RBX, [R13+96] ; no-arg-parsing entry point
; thread.binding-stack-pointer
; 2B: 48895DF8 MOV [RBP-8], RBX
; 2F: 840425F8FF1020 TEST AL, [#x2010FFF8] ; safepoint
; 36: BA02000000 MOV EDX, 2
; 3B: 488BFE MOV RDI, RSI
; 3E: 488B0593FFFFFF MOV RAX, [RIP-109] ; #<SB-KERNEL:FDEFN SB-KERNEL:%MAKE-LIST>
; 45: B904000000 MOV ECX, 4
; 4A: FF7508 PUSH QWORD PTR [RBP+8]
; 4D: FF6009 JMP QWORD PTR [RAX+9]
; 50: CC0F BREAK 15 ; Invalid argument count trap
NIL
*

但是请注意,(disassemble (lambda (x) (cons x nil)))(disassemble (lambda (x) (list x))) 似乎生成相同的代码。

(disassemble (lambda (x) (vector x)))(disassemble (lambda (x) (make-array 1 :initial-元素 x)))

listmake-list 之一(和 vectormake-array)更有效的跟随编译器优化?

此外,是listvector(以及make-listmake-array)之一 more高效(暂时忽略随后如何访问和更新序列)?

最佳答案

我有点不清楚如何比较(比方说)listmake-list,因为它们的目的完全不同。它们都返回一个列表这一事实并不能使它们相似或具有可比性。

因此,假设有人想了解 make-list 的执行情况。以下代码执行此操作:

* (let ((lst (time (make-list 10000000 :initial-element 0)))) (if lst t nil))
Evaluation took:
0.344 seconds of real time
0.343750 seconds of total run time (0.187500 user, 0.156250 system)
[ Run times consist of 0.251 seconds GC time, and 0.093 seconds non-GC time. ]
100.00% CPU
1,129,211,079 processor cycles
160,170,016 bytes consed

T
* (let ((lst (time (make-list 10000000 :initial-element 0)))) (if lst t nil))
Evaluation took:
0.188 seconds of real time
0.187500 seconds of total run time (0.125000 user, 0.062500 system)
[ Run times consist of 0.139 seconds GC time, and 0.049 seconds non-GC time. ]
100.00% CPU
632,759,465 processor cycles
160,195,440 bytes consed

T
* (let ((lst (time (make-list 10000000 :initial-element 0)))) (if lst t nil))
Evaluation took:
0.343 seconds of real time
0.343750 seconds of total run time (0.187500 user, 0.156250 system)
[ Run times consist of 0.266 seconds GC time, and 0.078 seconds non-GC time. ]
100.29% CPU
1,151,984,724 processor cycles
160,170,016 bytes consed

T
* (let ((lst (time (make-list 10000000 :initial-element 0)))) (if lst t nil))
Evaluation took:
0.203 seconds of real time
0.203125 seconds of total run time (0.171875 user, 0.031250 system)
[ Run times consist of 0.140 seconds GC time, and 0.064 seconds non-GC time. ]
100.00% CPU
648,536,502 processor cycles
160,195,520 bytes consed

T

首先要注意的是执行时间不一致,因为除了 GC 之外,系统中还有太多“随机”发生的事情。更重要的是,我们现在如何编写 10,000,000 个 0 列表传递给 list 进行比较?我们是否使用循环(在这种情况下,它是我们{主要}计时的循环)?我们是否首先创建打印表示一个 10,000,000 长的 0 列表,然后使用 read 读取(在这种情况下,我们将{主要}计时创建打印表示和 lisp 阅读器)?在我看来像苹果和橘子...

关于assembly - 比较 Common Lisp 中的 List/Make-list 和 Vector/Make-array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56924538/

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