diff --git a/multiprocessing/multiprocessing_exercise_0.ipynb b/multiprocessing/multiprocessing_exercise_0.ipynb index 124f889..e4be799 100644 --- a/multiprocessing/multiprocessing_exercise_0.ipynb +++ b/multiprocessing/multiprocessing_exercise_0.ipynb @@ -12,13 +12,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "656b87a0", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]\n" + ] + } + ], "source": [ "inputs = range(10)\n", - "... # TODO compute cubes\n", + "results = []\n", + "for i in inputs:\n", + " results.append(i ** 3)\n", "print(results)" ] }, @@ -33,15 +43,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "b3e934a0", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]\n" + ] + } + ], "source": [ "inputs = range(10)\n", - "... # TODO compute cubes\n", + "results = list(map(lambda x: x ** 3, inputs))\n", "print(results)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d246d75b", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -60,7 +86,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.4" + "version": "3.11.4" } }, "nbformat": 4, diff --git a/multiprocessing/multiprocessing_exercise_1.ipynb b/multiprocessing/multiprocessing_exercise_1.ipynb index 0121800..d22abdc 100644 --- a/multiprocessing/multiprocessing_exercise_1.ipynb +++ b/multiprocessing/multiprocessing_exercise_1.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "c6b53bbb", "metadata": {}, "outputs": [], @@ -23,6 +23,7 @@ " dx = (b - a) / n\n", " x = a + (i + 0.5) * dx\n", " y = f(x)\n", + " # s.append(y * dx)\n", " s = s + [y * dx]\n", " return sum(s)\n", "\n", @@ -51,14 +52,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "8f545181", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1.666661031407557e-09, 2.6666113761564247e-10, 6.666600604887662e-11, 2.96227486984435e-11]\n", + "CPU times: user 33.3 s, sys: 49.2 ms, total: 33.3 s\n", + "Wall time: 33.9 s\n" + ] + } + ], "source": [ "%%time\n", "ns = [10000, 25000, 50000, 75000]\n", - "... # TODO use a for loop\n", + "errors = [compute_error(n) for n in ns]\n", "print(errors)" ] }, @@ -76,7 +87,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "5a94fd97", "metadata": {}, "outputs": [], @@ -86,15 +97,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "479aee0f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1.666661031407557e-09, 2.6666113761564247e-10, 6.666600604887662e-11, 2.96227486984435e-11]\n", + "CPU times: user 7.63 ms, sys: 27.9 ms, total: 35.6 ms\n", + "Wall time: 29 s\n" + ] + } + ], "source": [ "%%time\n", - "... # TODO use the `ProcessPool`\n", + "with ProcessPool(processes=4) as pool:\n", + " errors = pool.map(compute_error, ns)\n", "print(errors)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fcf84dcb", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -113,7 +143,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.4" + "version": "3.11.4" } }, "nbformat": 4,