Does typing make python faster?
Type Hints are annotations that specify the runtime type of value within your Python codes. This look statically typed, right? Show
It’s intuitive to think there would be some performance (speed) improvement at runtime since statically typed languages do not intrinsically need to check data type during runtime. Right? Let’s check it out We will be using the classical Fibonacci series to check whether type hinting improves performance. FIBONACCI WITHOUT TYPE HINTFibonacci series without type hint.The Fibonacci function would be called 10,000 times and the appending operation would be called 10,000 * 100 (the nth term). Runtime profileThe program completed execution in approximately 0.705seconds. FIBONACCI WITH TYPE HINTINGUsing the same algorithm but with type hints. The program completed execution in approximately 0.708seconds. The execution times are very close and the difference might be caused by some CPU state.
Why Type Hints then?Python core philosophy as summarized in Zen of Python include:
Type Hints in Python would provide more readability to both human and statistical tools.
Readability COUNTS
When a developer starts writing Python code (without type hints), an IDE, like PyCharm, provides robust auto complete support. But as the number of lines and files increases, we start to notice the auto complete isn’t helpful at all. You are not getting those auto complete suggestions as they were earlier. What’s happening here? The reason this is happening is because the more code we encapsulate in functions, the more unclear variable data types get. Hence, with no information about variable data types, how can a linter function? To tackle this issue, Python incorporated PEP 484 into the Python 3.5 release. Through these hints, a programmer can now document what type of data is allowed in a variable. In this article, I am going to define type hints, what effect they have on the interpreter’s performance, and how they can help in documenting the code. Statically typed vs Dynamically typed languagesPrimarily, there are two types of programming languages. First are the statically typed languages such as C++ and Java, and second are the dynamically typed languages like JavaScript and Rust. Statically typed languages require variables to be declared with their data types before using them. On the contrary, dynamically typed languages work with variables declared during runtime. There is always a risk of encountering a runtime error in dynamically typed languages, so developers must take care of such scenarios by either using try-except or ensuring the return types. In this Python code, if a number doesn’t match with any logic, then by default None is returned. When this happens, a runtime AttributeError is raised because we are trying to use .lower(), a string attribute, on the None data type. Will using type hints make Python a statically typed language?No, type hints don’t force Python to check the type of data being stored in a variable. They are just for the developer’s reference and help auto-completion software. They allow IDEs to recognise data types and give suggestions based on this information. It also serves a purpose of documenting your code in a way that other developers need not read the entire snippet to know a variable’s data type. This gist explains how type hints are essentially ignored by Python interpreter. How to write type hints?The syntax goes like this
Type hints not only validate data types but can also be used with a class name to indicate a class instance. Using classes in type hints. Image by author.For instance, here the type hint provided is for class Specifying function return typesIn statically typed languages, functions must declare the type of data they are going to return. For example, in C++, a function that returns an integer would look like this. Addition of 2 numbers in c++Whereas in Python there is no such convention, a function can return any data type. This makes code difficult to debug and ambiguous. In Python, we do it like this, using a hyphen and greater than after closing parenthesis: Reiterating the previous point, function return type hints do not force functions to return specific data types; they are used as a means of documentation and readability. Does using type hints affect interpreter’s performance?There’s no effect on performance whatsoever with or without type hints. Cpython ignore type hints similar to how comments are discarded during runtime. ConclusionTo summarize the article, type hints:
Thanks for reading till the end! If you liked this article or are interested in topics like Automation and Python, follow me or subscribe to my newsletter. More content at plainenglish.io Is typing useful in Python?Type hints help you build and maintain a cleaner architecture. The act of writing type hints forces you to think about the types in your program. While the dynamic nature of Python is one of its great assets, being conscious about relying on duck typing, overloaded methods, or multiple return types is a good thing.
What is typing in Python?Typing defines a standard notation for Python function and variable type annotations. The notation can be used for documenting code in a concise, standard format, and it has been designed to also be used by static and runtime type checkers, static analyzers, IDEs and other tools.
What are reasons for using type hinting?Type hints will help document your code. And notice the idea of hints—they have no runtime effect, they're only hints and are not enforced on their own. Type hints were first specified in PEP 484, like I mentioned in the previous video, and they were first introduced into Python in version 3.5.
Does Cython improve performance?The CPython + Cython implementation is the fastest; it is 44 times faster than the CPython implementation. This is an impressive speed improvement, especially considering that the Cython code is very close to the original Python code in its design.
|