From a8b690821c936831a032d833b13dab81e849a228 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 23 Sep 2025 15:05:34 +0300 Subject: [PATCH] Simple modifications to power function --- exercise.ipynb | 131 ++++++++++++++++++++++++++++++------------------- 1 file changed, 81 insertions(+), 50 deletions(-) diff --git a/exercise.ipynb b/exercise.ipynb index f34ca00..81e48cc 100644 --- a/exercise.ipynb +++ b/exercise.ipynb @@ -2,15 +2,8 @@ "cells": [ { "cell_type": "code", - "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-03-04T09:40:28.904Z", - "iopub.status.busy": "2024-03-04T09:40:28.896Z", - "iopub.status.idle": "2024-03-04T09:40:28.978Z", - "shell.execute_reply": "2024-03-04T09:40:28.967Z" - } - }, + "execution_count": 1, + "metadata": {}, "outputs": [], "source": [ "import numpy as np" @@ -18,15 +11,8 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-03-04T10:02:39.062Z", - "iopub.status.busy": "2024-03-04T10:02:39.057Z", - "iopub.status.idle": "2024-03-04T10:02:39.068Z", - "shell.execute_reply": "2024-03-04T10:02:39.071Z" - } - }, + "execution_count": 2, + "metadata": {}, "outputs": [], "source": [ "n_series = 32\n", @@ -37,17 +23,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-03-04T10:02:41.027Z", - "iopub.status.busy": "2024-03-04T10:02:41.020Z", - "iopub.status.idle": "2024-03-04T10:02:41.036Z", - "shell.execute_reply": "2024-03-04T10:02:41.040Z" - }, - "scrolled": true - }, - "outputs": [], + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Size of one time series: 40 M\n", + "Size of collection: 1280 M\n", + "Gap size: 128 K\n", + "Gapped series size: 2 K\n" + ] + } + ], "source": [ "print(f'Size of one time series: {int(time_series[0].nbytes/2**20)} M')\n", "print(f'Size of collection: {int(time_series.nbytes/2**20)} M')\n", @@ -80,15 +69,8 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-03-04T10:06:08.461Z", - "iopub.status.busy": "2024-03-04T10:06:08.459Z", - "iopub.status.idle": "2024-03-04T10:06:08.466Z", - "shell.execute_reply": "2024-03-04T10:06:08.468Z" - } - }, + "execution_count": 4, + "metadata": {}, "outputs": [], "source": [ "# compute an approximation of a power series for a collection of gapped timeseries\n", @@ -100,6 +82,32 @@ " " ] }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "P = np.zeros_like(time_series)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5.53 s ± 1.3 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" + ] + } + ], + "source": [ + "%timeit power(time_series, P, gap)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -132,7 +140,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "def power_improved(time_series, P, gap):\n", + " for pwr in range(30):\n", + " for row_idx, row_values in enumerate(time_series):\n", + " P[row_idx] += (row_values[::gap]**pwr).sum()\n", + " return P" + ] + }, + { + "cell_type": "code", + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -163,21 +184,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-03-04T10:06:20.056Z", - "iopub.status.busy": "2024-03-04T10:06:20.053Z", - "iopub.status.idle": "2024-03-04T10:06:21.695Z", - "shell.execute_reply": "2024-03-04T10:06:21.700Z" + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "17.8 ms ± 1.56 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" + ] } - }, - "outputs": [], + ], "source": [ "P = np.zeros(n_series, dtype='float64')\n", "%timeit power_improved(time_series, P, gap)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Learnings:\n", + "- just changing the order of the loops: 5.5 s -> 24.2 ms\n", + "- also enumerating and accessing the rows: 17.8 ms" + ] + }, { "cell_type": "code", "execution_count": null, @@ -202,12 +233,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.13.5" + "version": "3.13.6" }, "nteract": { "version": "0.28.0" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 }