2024-heraklion-comp-arch/exercise.ipynb
2024-08-21 12:31:19 +02:00

4.8 KiB

In [ ]:
import numpy as np
In [ ]:
n_series = 32
len_one_series = 5*2**20
time_series = np.random.rand(n_series, len_one_series)
gap = 16*2**10
In [ ]:
print(f'Size of one time series: {int(time_series[0].nbytes/2**20)} M')
print(f'Size of collection: {int(time_series.nbytes/2**20)} M')
print(f'Gap size: {int(gap*8/2**10)} K')
print(f'Gapped series size: {int(time_series[0, ::gap].nbytes/2**10)} K')
In [ ]:
# compute a Taylor-like series
def taylor(time_series, mean, gap):
    for row, ts in enumerate(time_series):
        for pwr in range(1,20):
            mean[row] += (ts[::gap]**pwr).sum()
    return mean

Challenge

  • Can you improve on the above implementation of the taylor function?
  • Change the following taylor_improved function and see what you can do
  • Remember: you can't change any other cell in this notebook!
In [ ]:
def taylor_improved(time_series, mean, gap):
    for row, ts in enumerate(time_series):
        for pwr in range(1,20):
            mean[row] += (ts[::gap]**pwr).sum()
    return mean
In [ ]:
# verify that they yield the same results
out1 = taylor(time_series, np.zeros(n_series), gap)
out2 = taylor_improved(time_series, np.zeros(n_series), gap)
np.testing.assert_allclose(out1, out2)
In [ ]:
mean = np.zeros(n_series, dtype='float64')
%timeit taylor(time_series, mean, gap)
In [ ]:
mean = np.zeros(n_series, dtype='float64')
%timeit taylor_improved(time_series, mean, gap)