scope - prog 与 let in LISP 性能差异

标签 scope lisp let

有人告诉我,在 lisp 中,letprog 快(但 prog 有更多的灵 active ),当制作变量范围。我的问题是:为什么?我的意思是,有些时候使用 prog 更容易,但是,除了经验测试,我不知道如何猜测效果。是分配内存的时候吗?是执行吗?循环的时候会不会来得更多?我不知道实现差异的细节。

最佳答案

口齿不清

prog的描述说:

prog can be explained in terms of block, let, and tagbody as follows:

(prog variable-list declaration . body)
==  (block nil (let variable-list declaration (tagbody . body)))

换句话说,从功能上讲,proglet(除了一个小插图:前者返回 nil 而后者返回其最后形式的 value(s))。

一句谚语"sufficiently smart compiler" - 实际上, 任何现代 Lisp 编译器 - 可以检测到 returngo不使用并编译 prog 与等效的 let 相同:

(disassemble '(lambda () (let ((a 1) (b 2)) (print (+ a b)) nil)))

(disassemble '(lambda () (prog ((a 1) (b 2)) (print (+ a b)))))

产生相同的输出:

Disassembly of function :LAMBDA
(CONST 0) = 1
(CONST 1) = 2
0 required arguments
0 optional arguments
No rest parameter
No keyword parameters
7 byte-code instructions:
0     (CONST&PUSH 0)                      ; 1
1     (CONST&PUSH 1)                      ; 2
2     (CALLSR&PUSH 2 55)                  ; +
5     (PUSH-UNBOUND 1)
7     (CALLS1 142)                        ; PRINT
9     (NIL)
10    (SKIP&RET 1)
NIL

Cadence SKILL++

您可能想询问实现者。

关于scope - prog 与 let in LISP 性能差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49902313/

相关文章:

macros - 宏中的 clojure 引号和代字号

方案 : recursive process much faster than iterative

计划和让错误

Javascript:如何在 if block 内提升(或不提升)let?

c++ - 为什么派生类不能通过指向基类的指针访问其基类的 protected 成员?

jsf - 无状态 session Bean 和请求范围 Bean 有什么区别

perl - 不确定我是否正确使用了 Perl 的 Try::Tiny

Javascript:获取作用域变量

python - 在 Common Lisp 中管理依赖关系

lambda - 如何将 let* 表达为 lambda 表达式(不是常规的 let)