|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "f620fe00", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "import os, time\n", |
| 11 | + "\n", |
| 12 | + "# Generate 10MB\n", |
| 13 | + "data_len = 10000000\n", |
| 14 | + "data = os.urandom(data_len)\n", |
| 15 | + "file_ct = 1000\n", |
| 16 | + "file_len = int(data_len/file_ct)\n", |
| 17 | + "# Write one large file\n", |
| 18 | + "start = time.perf_counter()\n", |
| 19 | + "large_file = open(\"large.bin\", \"wb\")\n", |
| 20 | + "large_file.write(data)\n", |
| 21 | + "large_file.close ()\n", |
| 22 | + "large_write_s = time.perf_counter() - start\n", |
| 23 | + "# Write multiple small files\n", |
| 24 | + "start = time.perf_counter()\n", |
| 25 | + "for i in range(file_ct):\n", |
| 26 | + " small_file = open(f\"small_{i}.bin\", \"wb\")\n", |
| 27 | + " small_file.write(data[file_len*i:file_len*(i+1)])\n", |
| 28 | + " small_file.close()\n", |
| 29 | + "small_write_s = time.perf_counter() - start\n", |
| 30 | + "# Read back the large file\n", |
| 31 | + "start = time.perf_counter()\n", |
| 32 | + "large_file = open(\"large.bin\", \"rb\")\n", |
| 33 | + "t = large_file.read(data_len)\n", |
| 34 | + "large_file.close ()\n", |
| 35 | + "large_read_s = time.perf_counter() - start\n", |
| 36 | + "# Read back the small files\n", |
| 37 | + "start = time.perf_counter()\n", |
| 38 | + "for i in range(file_ct):\n", |
| 39 | + " small_file = open(f\"small_{i}.bin\", \"rb\")\n", |
| 40 | + " t = small_file.read(file_len)\n", |
| 41 | + " small_file.close()\n", |
| 42 | + "small_read_s = time.perf_counter() - start\n", |
| 43 | + "# Print Summary\n", |
| 44 | + "print(f\"{1:5d}x{data_len/1000000}MB Write: {large_write_s:.5f} seconds\")\n", |
| 45 | + "print(f\"{file_ct:5d}x{file_len/1000}KB Write: {small_write_s:.5f} seconds\")\n", |
| 46 | + "print(f\"{1:5d}x{data_len/1000000}MB Read: {large_read_s:.5f} seconds\")\n", |
| 47 | + "print(f\"{file_ct:5d}x{file_len/1000}KB Read: {small_read_s:.5f} seconds\")\n", |
| 48 | + "print(f\"{file_ct:5d}x{file_len/1000}KB Write was {small_write_s/large_write_s:.1f} slower than 1x{data_len/1000000}MB Write\")\n", |
| 49 | + "print(f\"{file_ct:5d}x{file_len/1000}KB Read was {small_read_s/large_read_s:.1f} slower than 1x{data_len/1000000}MB Read\")\n", |
| 50 | + "# Cleanup\n", |
| 51 | + "os.remove(\"large.bin\")\n", |
| 52 | + "for i in range(file_ct):\n", |
| 53 | + " os.remove(f\"small_{i}.bin\")" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": null, |
| 59 | + "id": "217267af", |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [], |
| 62 | + "source": [ |
| 63 | + "grid_shape = (512, 512)\n", |
| 64 | + "\n", |
| 65 | + "def update(grid, a_dt):\n", |
| 66 | + " x_max, y_max = grid_shape\n", |
| 67 | + " out_grid = [[0.0 for x in range(y_max)] * y_max for x in range(x_max)]\n", |
| 68 | + " for i in range(x_max):\n", |
| 69 | + " for j in range(y_max):\n", |
| 70 | + " out_xx = grid[(i-1)%x_max][j] - 2 * grid[i][j] + grid[(i+1)%x_max][j]\n", |
| 71 | + " out_yy = grid[i][(j-1)%y_max] - 2 * grid[i][j] + grid[i][(j+1)%y_max]\n", |
| 72 | + " out_grid[i][j] = grid[i][j] + (out_xx + out_yy) * a_dt \n", |
| 73 | + " return out_grid\n", |
| 74 | + " \n", |
| 75 | + "def heat_equation(steps):\n", |
| 76 | + " x_max, y_max = grid_shape\n", |
| 77 | + " grid = [[0.0] * y_max for x in range(x_max)]\n", |
| 78 | + " # Init central point to diffuse\n", |
| 79 | + " grid[int(x_max/2)][int(y_max/2)] = 1.0\n", |
| 80 | + " # Run steps\n", |
| 81 | + " for i in range(steps):\n", |
| 82 | + " grid = update(grid, 0.1)\n", |
| 83 | + "\n", |
| 84 | + "heat_equation(100)" |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "code", |
| 89 | + "execution_count": null, |
| 90 | + "id": "d0809621", |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [], |
| 93 | + "source": [ |
| 94 | + "grid_shape = (512, 512)\n", |
| 95 | + "\n", |
| 96 | + "def update(grid, a_dt, out_grid):\n", |
| 97 | + " x_max, y_max = grid_shape\n", |
| 98 | + " for i in range(x_max):\n", |
| 99 | + " for j in range(y_max):\n", |
| 100 | + " out_xx = grid[(i-1)%x_max][j] - 2 * grid[i][j] + grid[(i+1)%x_max][j]\n", |
| 101 | + " out_yy = grid[i][(j-1)%y_max] - 2 * grid[i][j] + grid[i][(j+1)%y_max]\n", |
| 102 | + " out_grid[i][j] = grid[i][j] + (out_xx + out_yy) * a_dt \n", |
| 103 | + " \n", |
| 104 | + "def heat_equation(steps):\n", |
| 105 | + " x_max, y_max = grid_shape\n", |
| 106 | + " grid = [[0.0 for x in range(y_max)] for x in range(x_max)]\n", |
| 107 | + " out_grid = [[0.0 for x in range(y_max)] for x in range(x_max)] # Allocate a second buffer once\n", |
| 108 | + " # Init central point to diffuse\n", |
| 109 | + " grid[int(x_max/2)][int(y_max/2)] = 1.0\n", |
| 110 | + " # Run steps\n", |
| 111 | + " for i in range(steps):\n", |
| 112 | + " update(grid, 0.1, out_grid) # Pass the output buffer\n", |
| 113 | + " grid, out_grid = out_grid, grid # Swap buffers\n", |
| 114 | + "\n", |
| 115 | + "heat_equation(100)" |
| 116 | + ] |
| 117 | + } |
| 118 | + ], |
| 119 | + "metadata": {}, |
| 120 | + "nbformat": 4, |
| 121 | + "nbformat_minor": 5 |
| 122 | +} |
0 commit comments