默认情况下实现本地作用域的成本
众所周知,在某些语言中(最著名的例子是 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 Lua 语言更好的一些要点(默认本地或默认全局)可以在 在此 wiki 页面。也许默认情况下两者都不是最好的答案,但我们程序员希望节省一些打字;)
维基页面的一些引用:
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:
在编译时,本地默认和全局默认的成本是相同的。当您找到尚未见过的名称时,您仍然必须完全遍历所有活动局部变量的列表。在运行时,局部变量的访问速度通常更快。
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.