Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Optimizations

In this lesson, you’ll learn about new optimizations made for Python 3.8. There are several optimizations made for Python 3.8, some on which make code run faster. Others reduce the memory footprint. For example, looking up fields in a namedtuple is significantly faster in Python 3.8 compared with Python 3.7:

>>>
>>> import collections
>>> from timeit import timeit
>>> Person = collections.namedtuple("Person", "name twitter")
>>> raymond = Person("Raymond", "@raymondh")

>>> # Python 3.7
>>> timeit("raymond.twitter", globals=globals())
0.06228263299999526

>>> # Python 3.8
>>> timeit("raymond.twitter", globals=globals())
0.03338557700000422

You can see that looking up .twitter on the namedtuple is 30% to 40% faster in Python 3.8. Lists save some space when they are initialized from iterables with a known length. This can save memory:

>>>
>>> import sys

>>> # Python 3.7
>>> sys.getsizeof(list(range(20191014)))
181719232

>>> # Python 3.8
>>> sys.getsizeof(list(range(20191014)))
161528168

In this case, the list uses about 11% less memory in Python 3.8 compared with Python 3.7.

Other optimizations include better performance in subprocess, faster file copying with shutil, improved default performance in pickle, and faster operator.itemgetter operations. See the official documentation for a complete list of optimizations.

Pygator on Nov. 28, 2019

Why do you have to pass in globals=globals()?

Geir Arne Hjelle RP Team on Nov. 28, 2019

By default, timeit() runs within its own namespace, where it does not have access to variables or functions you have defined. There are essentially two ways you can give it access to objects like raymond in the example:

  1. Using setup=... to create all necessary variables or object
  2. Using globals=... to change which namespace timeit() runs within

For simple exploration, option 2 is usually easier. Option 1 gives you slightly better control, as it’s easier to keep track of what’s in the setup statement than to have full control of what’s in a namespace. However, for examples like these option 2 is more than adequate.

Using globals=globals() is particularly easy when exploring, as that effectively tells timeit() to run within the current global namespace.

See the documentation for more details about how timeit() works.

AriadneAnne Tsambali on Nov. 30, 2019

Dear Ladies and Gentlemen,

I love coding and I am a passioner with computing. First of All, I would like to thank you for your amazing offer and your suprise idea. God Bless you All. Please think Us and on the next year around. Whatever you offer it makes good. Once more time thank you all as I pleased with your present/gift. Thank you once more and I wish you to have a good weekend.

Truly Yours

Ariadne-Anne Tsambali Phd Researcher

Become a Member to join the conversation.