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()?