# Python Type Hints and Speed

<span class="w"></span>

# 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?

![Image for post](https://miro.medium.com/max/60/1*llWP7DY29O8VF8gbe1pd2g.png?q=20)

<noscript><img alt="Image for post" class="dx hg dy hh t" src="https://miro.medium.com/max/1410/1*llWP7DY29O8VF8gbe1pd2g.png" width="705" height="136" srcSet="https://miro.medium.com/max/552/1*llWP7DY29O8VF8gbe1pd2g.png 276w, https://miro.medium.com/max/1104/1*llWP7DY29O8VF8gbe1pd2g.png 552w, https://miro.medium.com/max/1280/1*llWP7DY29O8VF8gbe1pd2g.png 640w, https://miro.medium.com/max/1400/1*llWP7DY29O8VF8gbe1pd2g.png 700w" sizes="700px"/></noscript>

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

<span class="ie fi bk if ig ih"></span><span class="ie fi bk if ig ih"></span><span class="ie fi bk if ig"></span>

We will be using the classical Fibonacci series to check whether type hinting improves performance.

# FIBONACCI WITHOUT TYPE HINT

![Image for post](https://miro.medium.com/max/60/1*QgkKyUtR30MogppNmFyfiA.png?q=20)

<noscript><img alt="Image for post" class="dx hg dy hh t" src="https://miro.medium.com/max/2090/1*QgkKyUtR30MogppNmFyfiA.png" width="1045" height="313" srcSet="https://miro.medium.com/max/552/1*QgkKyUtR30MogppNmFyfiA.png 276w, https://miro.medium.com/max/1104/1*QgkKyUtR30MogppNmFyfiA.png 552w, https://miro.medium.com/max/1280/1*QgkKyUtR30MogppNmFyfiA.png 640w, https://miro.medium.com/max/1400/1*QgkKyUtR30MogppNmFyfiA.png 700w" sizes="700px"/></noscript>

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).

![Image for post](https://miro.medium.com/max/60/1*xQKlcw-zu1n4py4r5d1oeg.png?q=20)

<noscript><img alt="Image for post" class="dx hg dy hh t" src="https://miro.medium.com/max/2260/1*xQKlcw-zu1n4py4r5d1oeg.png" width="1130" height="699" srcSet="https://miro.medium.com/max/552/1*xQKlcw-zu1n4py4r5d1oeg.png 276w, https://miro.medium.com/max/1104/1*xQKlcw-zu1n4py4r5d1oeg.png 552w, https://miro.medium.com/max/1280/1*xQKlcw-zu1n4py4r5d1oeg.png 640w, https://miro.medium.com/max/1400/1*xQKlcw-zu1n4py4r5d1oeg.png 700w" sizes="700px"/></noscript>

Runtime profile

The program completed execution in approximately _0.705seconds_.

# FIBONACCI WITH TYPE HINTING

Using <span id="rmm"></span> the same algorithm but with type hints.

![Image for post](https://miro.medium.com/max/60/1*5jQiEy9Kkp-bEFK6oOnclg.png?q=20)

<noscript><img alt="Image for post" class="dx hg dy hh t" src="https://miro.medium.com/max/2254/1*5jQiEy9Kkp-bEFK6oOnclg.png" width="1127" height="713" srcSet="https://miro.medium.com/max/552/1*5jQiEy9Kkp-bEFK6oOnclg.png 276w, https://miro.medium.com/max/1104/1*5jQiEy9Kkp-bEFK6oOnclg.png 552w, https://miro.medium.com/max/1280/1*5jQiEy9Kkp-bEFK6oOnclg.png 640w, https://miro.medium.com/max/1400/1*5jQiEy9Kkp-bEFK6oOnclg.png 700w" sizes="700px"/></noscript>

The program completed execution in approximately _0.708seconds_.

The execution times are very close and the difference might be caused by some CPU state.

> [It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention.](https://www.python.org/dev/peps/pep-0484/)

# Why Type Hints then?

Python core philosophy as summarized in [Zen of Python](https://en.wikipedia.org/wiki/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.

> [This PEP[484] aims to provide a standard syntax for type annotations, opening up Python code to easier static analysis and refactoring, potential runtime type checking, and (perhaps, in some contexts) code generation utilizing type information.](https://www.python.org/dev/peps/pep-0484/)

# Readability COUNTS
