默认情况下实现本地作用域的成本

发布于 2024-11-27 10:51:53 字数 348 浏览 1 评论 0原文

众所周知,在某些语言中(最著名的例子是 javascript),变量默认是全局范围的。这意味着如果想声明局部变量,他应该写 var, local, my 或其他。

我从来没有考虑过这样做的实施成本,但事实证明这可能不仅仅是一个传统问题。例如,检查链接。我的问题是 - 默认本地范围架构是否比默认全局范围架构更昂贵。只是有点,不知道,事先选择排序比冒泡排序需要更少的交换,以这种方式“事先”。

此外,如果有人编辑这个问题以添加适当的标签,我将不胜感激。我只是不知道哪一个更适合这里。

As we all know, in some languages (the most known example is javascript) variables are global scoped by default. That means that if one wants to declare local variable, he should write var, local, my or whatever.

I'd never thought about the implementation costs of this, but it turns out that it could be not just a matter of traditions. For example, check this link. My question is - is local scope-by-default architecture beforehand more pricey than global-scope-by-default. Just kinda of, don't know, selection sort beforehand requires less swaps than bubblesort, in that way "beforehand".

Besides, I would appreciate if somebody will edit this question to add appropriate tags. I just don't know which one fits better here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2024-12-04 10:51:53

对于 Lua 语言更好的一些要点(默认本地或默认全局)可以在 在此 wiki 页面。也许默认情况下两者都不是最好的答案,但我们程序员希望节省一些打字;)

维基页面的一些引用:

  • “默认情况下本地是错误的。也许默认情况下全局也是错误的,[但是]解决方案不是默认为本地。” (Roberto Ierusalimschy,Lua 架构师)
  • “[当前的局部变量作用域规则] 是 Ruby 中最大的设计缺陷。” (Yukihiro Matsumoto,Ruby 设计师)
  • “我不喜欢词法的隐式声明,因为它往往会破坏它们的主要用途,即捕获拼写错误...声明应该看起来像声明...我相信带有 my 的声明 被正确的霍夫曼编码。” (Larry Wall,Perl 设计师)

A summary of some points which is better (local-by-default or global-by-default) for the Lua language can be found on this wiki page. Maybe neither-by-default is the best answer, but we programmers want to save some typing ;)

Some quotes from the wiki page:

  • "Local by default is wrong. Maybe global by default is also wrong, [but] the solution is not local by default." (Roberto Ierusalimschy, architect of Lua)
  • "[The current local variable scoping rule] is the single biggest design flaw in Ruby." (Yukihiro Matsumoto, Ruby designer)
  • "I dislike the implicit declaration of lexicals because it tends to defeat the primary use of them, namely, catching typos...Declarations should look like declarations...I believe that declarations with my are properly Huffman encoded." (Larry Wall, Perl Designer)
天生の放荡 2024-12-04 10:51:53

在编译时,本地默认和全局默认的成本是相同的。当您找到尚未见过的名称时,您仍然必须完全遍历所有活动局部变量的列表。在运行时,局部变量的访问速度通常更快。

At compile time, the costs of local-by-default and global-by-default are the same. You still have to completely traverse the list of all active local variables when you find a name that hasn't been seen yet. At run time, local variables are usually faster to access.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文