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 ?
It's because statically typed languages are often compiled to machine code while dynamically typed languages are in most cases run by an interpreter.
Certain types of compile time optimizations can only be performed if a variable's exact type is known.
Dynamically typed languages also often have added logic to determine the type and to ensure that the value is correct for the type.
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.
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.
Dynamically typed languages must make all of their checks at runtime because the type might change during the course of the execution.
Static typed languages resolve all types during compile time so the cost is consumed up front, one time.
This is the main reason why dynamic typed languages are typically slower. But there are other things to think about. A lot depends on the compiler or interpreter, the GC implementation, the dispatch table layout and lookup algorithms along with other optimizations.
It all depends on the implementation: A dynamic typed language can be faster than a compiled language it just takes more work to accomplish this.
Your question is a bit off since dynamically typed languages actually aren't slow. Many examples may be in practice, but others are fast (where fast means "reasonably comparable to c" or something like that, cf common lisp).
Many dynamic languages are running on a VM or even interpreted, which may be causing slowdowns that are avoidable. At a certain level, there are optimizations that are available to static language compilers (or dynamic ones that have made the right sorts of promises not to be dynamic about something) that aren't possible in a fully dynamic situation.
However, if you're thinking about the differences between say, python and c++ just for example, it's not the dynamic vs. static that is really the issue.