Update page 'Day 7 homework - Sloejtes'
@@ -0,0 +1,62 @@
|
||||
### Sloetjes\_Day-7\_Homework\_04/12/23
|
||||
|
||||
**1. Use the `time.perf_counter_ns()` method to measure how many nanoseconds it takes for `time.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.**
|
||||
|
||||
```
|
||||
import time
|
||||
|
||||
durations = [0.01, 0.1, 1]
|
||||
repeats = 100
|
||||
|
||||
for duration in durations:
|
||||
times = []
|
||||
for i in range(repeats):
|
||||
start_time = time.perf_counter_ns()
|
||||
time.sleep(duration)
|
||||
end_time = time.perf_counter_ns()
|
||||
times.append(end_time - start_time)
|
||||
print(f"Duration: {duration}s, Mean: {sum(times)/len(times)} ns, Max: {max(times)} ns, Min: {min(times)} ns")
|
||||
```
|
||||
###### Output:
|
||||
```
|
||||
Duration: 0.01s, Mean: 12103232.93 ns, Max: 12948458 ns, Min: 10101042 ns
|
||||
Duration: 0.1s, Mean: 103480802.47 ns, Max: 107881250 ns, Min: 100116125 ns
|
||||
Duration: 1s, Mean: 1003430530.03 ns, Max: 1005073459 ns, Min: 1000114667 ns
|
||||
```
|
||||
###### Visualization of the jitter:
|
||||
```
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
bins = 10
|
||||
|
||||
for duration in durations:
|
||||
times = []
|
||||
for i in range(repeats):
|
||||
start_time = time.perf_counter_ns()
|
||||
time.sleep(duration)
|
||||
end_time = time.perf_counter_ns()
|
||||
times.append(end_time - start_time)
|
||||
plt.hist(times, bins=bins)
|
||||
plt.title(f"Duration: {duration}s")
|
||||
plt.xlabel("Time difference (ns)")
|
||||
plt.ylabel("Frequency")
|
||||
plt.show()
|
||||
```
|
||||

|
||||

|
||||

|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
2. Create a dict with 1 million integer keys, each with 64 bytes of random values.
|
||||
3. 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?
|
||||
4. The ABC module can be used to create so-called *virtual base classes*, similar to the `Simulator.py` file 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 `@abstractmethod` and `@property` decorators to define the shared methods and properties of the class, and non-abstract methods and properties which are the same for the two objects.
|
||||
5. 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
|
||||
6. What happens when you try to call an abstract method, or access an abstract property in the base class?
|
||||
Reference in New Issue
Block a user