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 ?
The main disadvantages of dynamic typing are: Run-time type errors. Can be very difficult or even practically impossible to achieve the same level of correctness and requires vastly more testing. No compiler-verified documentation.
A language is dynamically typed if the type is associated with run-time values, and not named variables/fields/etc. This means that you as a programmer can write a little quicker because you do not have to specify types every time (unless using a statically-typed language with type inference).
Static typing usually results in compiled code that executes more quickly because when the compiler knows the exact data types that are in use, it can produce optimized machine code (i.e. faster and/or using less memory).
No. Dynamic languages are not slower than static languages. In fact, it is impossible for any language, dynamic or not, to be slower than another language (or faster, for that matter), simply because a language is just a bunch of abstract mathematical rules.
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.
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.
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.
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.
Look at this python example:
def fact(n): if n==0: return n return n*fact(n-1)
What is n? Is it a number? Is it a String? Is it a class you defined earlier? There is no way for the compiler to know what input it will get. You have to do a lot of checking at run-time, which means that you are doing more implicit work for simple operations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With