views:

536

answers:

6

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 ?

A: 

It's because statically typed languages are often compiled to machine code while dynamically typed languages are in most cases run by an interpreter.

Darin Dimitrov
Seems to be oversimplifying a little bit. Not all statically typed languages compile down to machine code, including any static CLR or JVM language. Not all dynamically typed languages are interpreted, for example many Lisps can be compiled down to bytecode, and there are many compilers for Python and PHP as well.
Juliet
Common Lisp can actually be compiled to machine code.
John Millikin
Most good Common Lisp implementations compile to machine code. Some don't have an interpreter, they just do JIT compilation.
David Thornley
+7  A: 

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.

Ben S
+5  A: 

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.

bigmonachus
Actually some languagse, like Ocaml, F#, and Haskell support type-inference (http://en.wikipedia.org/wiki/Type_inference), so the compiler can determine the datatype of a variable based on its usage without type-annotations. For example, 'n==0', an equality test to an integer literal indicates that 'n' is an integer. Since the compiler knows 'n' is an integer, then 'return n' implies the function returns an integer as well. So, we can determine that the function takes an int and returns an int.
Juliet
@Princess: Ah, but is it a machine-sized int or a bignum? You still have to test this (but it can be done efficiently)
simon
+13  A: 

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.

John Millikin
It's possible to specify types statically in Common Lisp, when you feel the need. An implementation built for speed can produce very fast code.
David Thornley
+2  A: 

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.

Robert Kozak
There can be compiled dynamic languages ;-)
Wallacoloo
+1  A: 

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.

simon
It may also be nice to note that some of, for instance, the more recent Javascript bytecode translators often perform as fast as C++.
eyelidlessness