1
Day 7 homework
j edited this page 2023-04-12 10:24:29 +02:00
Today's homework is to do some performance analysis of Python's hash tables, which are a key data structure used by the dict class. We're also going to do some miscellaneous tasks to help practice writing Python.
- Use the
time.perf_counter_ns()method to measure how many nanoseconds it takes fortime.sleep()to run for the following durations: 10 milliseconds, 100 milliseconds, 1 second. Repeat for 100 times for each of those durations. The variation in timing is called jitter. Can you find any pattern to how the jitter varies for each of the different sleep durations? Binning the results and creating a histogram may help. - Create a dict with 1 million integer keys, each with 64 bytes of random values.
- Now that we know how to precisely time how long it takes for Python to do something, measure how long it takes to look up 1,000, 10,000, and 100,000 random integer keys, and repeat each experiment 100 times. Does the amount of time per key lookup change?
- The ABC module can be used to create so-called virtual base classes, similar to the
Simulator.pyfile from day 4. Look around you, and find two related objects. Create a virtual base class by inheriting from the ABC module and using the@abstractmethodand@propertydecorators to define the shared methods and properties of the class, and non-abstract methods and properties which are the same for the two objects. - Create concrete classes which inherit from your virtual base class from the previous task, and define the non-abstract methods and properties which are different for each of the two objects. I'm deliberately omitting an example here to incentivize you to read the documentation on the ABC module: https://docs.python.org/3/library/abc.html#module-abc
- What happens when you try to call an abstract method, or access an abstract property in the base class?