Skip to content

Commit 341b22f

Browse files
committed
automatically generated notebooks
1 parent e571c97 commit 341b22f

7 files changed

+864
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "3b8b3bc6",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from timeit import timeit\n",
11+
"\n",
12+
"def list_append():\n",
13+
" li = []\n",
14+
" for i in range(100000):\n",
15+
" li.append(i)\n",
16+
"\n",
17+
"def list_preallocate():\n",
18+
" li = [0]*100000\n",
19+
" for i in range(100000):\n",
20+
" li[i] = i\n",
21+
"\n",
22+
"def list_comprehension():\n",
23+
" li = [i for i in range(100000)]\n",
24+
"\n",
25+
"repeats = 1000\n",
26+
"print(f\"Append: {timeit(list_append, number=repeats):.2f}ms\")\n",
27+
"print(f\"Preallocate: {timeit(list_preallocate, number=repeats):.2f}ms\")\n",
28+
"print(f\"Comprehension: {timeit(list_comprehension, number=repeats):.2f}ms\")"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": null,
34+
"id": "31a74be6",
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"class MyKey:\n",
39+
"\n",
40+
" def __init__(self, _a, _b, _c):\n",
41+
" self.a = _a\n",
42+
" self.b = _b\n",
43+
" self.c = _c\n",
44+
"\n",
45+
" def __eq__(self, other):\n",
46+
" return (isinstance(other, type(self))\n",
47+
" and (self.a, self.b, self.c) == (other.a, other.b, other.c))\n",
48+
"\n",
49+
" def __hash__(self):\n",
50+
" return hash((self.a, self.b, self.c))\n",
51+
"\n",
52+
"dict = {}\n",
53+
"dict[MyKey(\"one\", 2, 3.0)] = 12"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": null,
59+
"id": "8bbf7001",
60+
"metadata": {},
61+
"outputs": [],
62+
"source": [
63+
"import random\n",
64+
"from timeit import timeit\n",
65+
"\n",
66+
"def generateInputs(N = 25000):\n",
67+
" random.seed(12) # Ensure every list is the same \n",
68+
" return [random.randint(0,int(N/2)) for i in range(N)]\n",
69+
" \n",
70+
"def uniqueSet():\n",
71+
" ls_in = generateInputs()\n",
72+
" set_out = set(ls_in)\n",
73+
" \n",
74+
"def uniqueSetAdd():\n",
75+
" ls_in = generateInputs()\n",
76+
" set_out = set()\n",
77+
" for i in ls_in:\n",
78+
" set_out.add(i)\n",
79+
" \n",
80+
"def uniqueList():\n",
81+
" ls_in = generateInputs()\n",
82+
" ls_out = []\n",
83+
" for i in ls_in:\n",
84+
" if not i in ls_out:\n",
85+
" ls_out.append(i)\n",
86+
"\n",
87+
"def uniqueListSort():\n",
88+
" ls_in = generateInputs()\n",
89+
" ls_in.sort()\n",
90+
" ls_out = [ls_in[0]]\n",
91+
" for i in ls_in:\n",
92+
" if ls_out[-1] != i:\n",
93+
" ls_out.append(i)\n",
94+
" \n",
95+
"repeats = 1000\n",
96+
"gen_time = timeit(generateInputs, number=repeats)\n",
97+
"print(f\"uniqueSet: {timeit(uniqueSet, number=repeats)-gen_time:.2f}ms\")\n",
98+
"print(f\"uniqueSetAdd: {timeit(uniqueSetAdd, number=repeats)-gen_time:.2f}ms\")\n",
99+
"print(f\"uniqueList: {timeit(uniqueList, number=repeats)-gen_time:.2f}ms\")\n",
100+
"print(f\"uniqueListSort: {timeit(uniqueListSort, number=repeats)-gen_time:.2f}ms\")"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": null,
106+
"id": "63d3eb19",
107+
"metadata": {},
108+
"outputs": [],
109+
"source": [
110+
"import random\n",
111+
"from timeit import timeit\n",
112+
"from bisect import bisect_left\n",
113+
"\n",
114+
"N = 25000 # Number of elements in list\n",
115+
"M = 2 # N*M == Range over which the elements span\n",
116+
"\n",
117+
"def generateInputs():\n",
118+
" random.seed(12) # Ensure every list is the same\n",
119+
" st = set([random.randint(0, int(N*M)) for i in range(N)])\n",
120+
" ls = list(st)\n",
121+
" ls.sort() # Sort required for binary\n",
122+
" return st, ls # Return both set and list\n",
123+
" \n",
124+
"def search_set():\n",
125+
" st, _ = generateInputs()\n",
126+
" j = 0\n",
127+
" for i in range(0, int(N*M), M):\n",
128+
" if i in st:\n",
129+
" j += 1\n",
130+
" \n",
131+
"def linear_search_list():\n",
132+
" _, ls = generateInputs()\n",
133+
" j = 0\n",
134+
" for i in range(0, int(N*M), M):\n",
135+
" if i in ls:\n",
136+
" j += 1\n",
137+
" \n",
138+
"def binary_search_list():\n",
139+
" _, ls = generateInputs()\n",
140+
" j = 0\n",
141+
" for i in range(0, int(N*M), M):\n",
142+
" k = bisect_left(ls, i)\n",
143+
" if k != len(ls) and ls[k] == i:\n",
144+
" j += 1\n",
145+
"\n",
146+
" \n",
147+
"repeats = 1000\n",
148+
"gen_time = timeit(generateInputs, number=repeats)\n",
149+
"print(f\"search_set: {timeit(search_set, number=repeats)-gen_time:.2f}ms\")\n",
150+
"print(f\"linear_search_list: {timeit(linear_search_list, number=repeats)-gen_time:.2f}ms\")\n",
151+
"print(f\"binary_search_list: {timeit(binary_search_list, number=repeats)-gen_time:.2f}ms\")"
152+
]
153+
}
154+
],
155+
"metadata": {},
156+
"nbformat": 4,
157+
"nbformat_minor": 5
158+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "6438a865",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"# Python code\n",
11+
"a = 1\n",
12+
"b = 2\n",
13+
"c = a + b"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": null,
19+
"id": "b38dca50",
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"# file: test_demonstration.py\n",
24+
"\n",
25+
"# A simple function to be tested, this could instead be an imported package\n",
26+
"def squared(x):\n",
27+
" return x**2\n",
28+
"\n",
29+
"# A simple test case\n",
30+
"def test_example():\n",
31+
" assert squared(5) == 24"
32+
]
33+
}
34+
],
35+
"metadata": {},
36+
"nbformat": 4,
37+
"nbformat_minor": 5
38+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

Comments
 (0)