为什么动态类型语言很慢? [英] Why are dynamically typed languages slow?

查看:65
本文介绍了为什么动态类型语言很慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与静态类型语言相比,是什么让动态类型语言难以加速.换句话说,静态类型语言的内在属性是什么,使其易于优化执行速度?

What makes it hard to speed up dynamically typed languages when compared to statically typed languages . In other words what is inherent property of statically typed languages that make them easy to optimize for speed of execution ?

推荐答案

在静态类型语言中访问属性/方法时,查找通常可以减少到静态函数地址.即使在较慢的虚方法的情况下,查找也只是从 vtable 中读取偏移量.

When accessing attributes / methods in statically-typed languages, lookups can usually be reduced to a static function address. Even in the case of virtual methods, which are slower, the lookup is just reading an offset from a vtable.

在动态语言中,名称基于字符串.想要查找 foo.bar?在局部变量哈希表中找到foo,然后在foo的哈希表中找到bar.在某些动态语言(如 Python 和 Ruby)中,可能会有额外的查找/方法调用来实现动态生成的属性.

In dynamic languages, names are based on strings. Want to look up foo.bar? Find foo in the local variable hash table, then find bar in foo's hash table. In some dynamic languages, like Python and Ruby, there may be additional lookups/method calls to implement dynamically generated attributes.

所有这些查找都非常很难快速完成.Python 拥有世界上调优得最好的哈希表实现之一,而 JavaScript 投入了数百万美元的研究资金来加快速度.这些策略有效——将 Chrome 的 JavaScript 与 IE 5 进行比较,看看有多少——但它们比静态生成函数调用要困难得多.

All of these lookups are very hard to make fast. Python has one of the most well-tuned hash table implementations in the world, and JavaScript has had millions of dollars of research money poured into making it fast. These tactics work -- compare Chrome's JavaScript with IE 5 to see just how much -- but they are much, much more difficult than just statically generating function calls.

我应该提到一种语言的动态"程度可能会有所不同.Python 有几种不同的方式与变量查找进行交互,这在某些情况下很好,但会使优化变得非常困难.其他动态语言(例如 Common Lisp 和 Smalltalk)在许多用例中可以与静态语言平分秋色,因为动态查找/修改受到更多控制.

I should mention that how "dynamic" a language is can vary. Python has several different ways to interact with variable lookups, which is nice in some circumstances, but makes optimization very hard. Other dynamic languages, such as Common Lisp and Smalltalk, can compete evenly with static languages in many use cases because dynamic lookups/modifications are more controlled.

这篇关于为什么动态类型语言很慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆