Home > OS >  Does strict typing increase Python program performance?
Does strict typing increase Python program performance?

Time:11-21

I'm a beginner and can't find an answer to this question on google.

Based on threads like this What makes C faster than Python? I've learned that dynamic/static typing isn't the main reason that C is faster than Python. It appears to be largely because python programs are interpreted, and c programs are compiled.

I'm wondering if strict typing would close the gap in performance for interpreted vs compiled languages enough that strict typing would be a viable strategy for improving Python performance post facto?

If the answer is yes, is this done in pro-dev contexts?

CodePudding user response:

With current versions of Python, type annotations are mostly hints for the programmer and possibly some validation tools but are ignored by the compiler and absent at runtime, which is similar to the behavior of Typescript.

It might be possible to change the semantics of Python to take advantage of static typing in some circumstances to generate more efficient byte-code and possibly perform just in time executable code generation (JIT). Advanced Javascript engines use complex heuristics to achieve this without type annotations. Both approaches could help make Python programs much faster and in some cases perform better than equivalent C code.

Note also that many advanced Python packages use native code, written in C and other languages, taking advantage of optimizing compilers, SIMD instructions and even multi-threading... The Python code in programs using these libraries is not where the time is spent and the performance is comparable to that of compiled languages, while giving the programmer a simpler language to express their problems.

  • Related