Почему динамически типизированные языки являются медленными?

В Windows вам не нужна библиотека для загрузки файла BMP, так как эта функция встроена в операционную систему. Вы можете использовать функцию LoadImage .

17
задан Jon Limjap 17 April 2009 в 17:36
поделиться

6 ответов

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.

25
ответ дан 30 November 2019 в 10:31
поделиться

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.

12
ответ дан 30 November 2019 в 10:31
поделиться

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.

10
ответ дан 30 November 2019 в 10:31
поделиться

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.

5
ответ дан 30 November 2019 в 10:31
поделиться

Ваш вопрос немного неактуален, поскольку языки с динамической типизацией на самом деле не медленные. На практике может быть много примеров, но другие бывают быстрыми (где быстро означает «достаточно сопоставимо с c» или что-то в этом роде, ср. Common lisp).

Многие динамические языки работают на виртуальной машине или даже интерпретируются, что может вызывать замедление, которого можно избежать. На определенном уровне существуют оптимизации, доступные для компиляторов статического языка (или динамических компиляторов, которые давали правильные обещания не быть динамическими в отношении чего-либо), которые невозможны в полностью динамической ситуации.

Однако, если вы думаете о различиях, например, между Python и C ++, то на самом деле проблема не в динамическом и статическом.

1
ответ дан 30 November 2019 в 10:31
поделиться

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

0
ответ дан 30 November 2019 в 10:31
поделиться
Другие вопросы по тегам:

Похожие вопросы: