Python Type Hints and Speed
Python Type Hinting and Speed
Type Hints are annotations that specify the runtime type of value within your Python codes. This look statically typed, right?

This was specified in PEP 484 and introduced Python 3.5.
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 HINT

Fibonacci 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 profile
The program completed execution in approximately 0.705seconds.
FIBONACCI WITH TYPE HINTING
Using 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:
- Beautiful is better than ugly.
- Explicit is better than implicit.
- Simple is better than complex.
- Complex is better than complicated.
- Readability counts.
Type Hints in Python would provide more readability to both human and statistical tools.


![Deploy and Access Flask App on Windows Server [No CGI]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1639120854833%2Ft8KDePUF_.png&w=3840&q=75)