Skip to content

Commit

Permalink
Updating course content
Browse files Browse the repository at this point in the history
  • Loading branch information
debakarr committed Aug 16, 2023
1 parent 2e7ba1a commit 43a4d95
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 1 deletion.
141 changes: 140 additions & 1 deletion content/02_memory_management_in_python.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,145 @@
"asizeof.asizeof([1, 2, [1, 2, 3, 4]])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "47d3f8ec-d600-4690-9586-96282fa7538e",
"metadata": {},
"outputs": [],
"source": [
"%pip install scalene"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4757a6c4-9a01-47a4-acb9-874a6cbf0d76",
"metadata": {},
"outputs": [],
"source": [
"%%writefile example/concate_with_plus.py\n",
"def add_string_with_plus(iters):\n",
" s = \"\"\n",
" for i in range(iters):\n",
" s += \"abc\"\n",
" assert len(s) == 3*iters\n",
" \n",
"add_string_with_plus(500000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "61ac2488-6c3f-44ea-a0b9-a0e3319218c1",
"metadata": {},
"outputs": [],
"source": [
"%%writefile example/concate_with_join.py\n",
"def add_string_with_join(iters):\n",
" l = []\n",
" for i in range(iters):\n",
" l.append(\"abc\")\n",
" s = \"\".join(l)\n",
" assert len(s) == 3*iters\n",
" \n",
"add_string_with_join(500000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d7a1af7e-66f9-4336-beda-f4b011b7aee1",
"metadata": {},
"outputs": [],
"source": [
"!scalene example/concate_with_plus.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2458770-7cf8-403c-876c-20839788d1f7",
"metadata": {},
"outputs": [],
"source": [
"!scalene example/concate_with_join.py"
]
},
{
"cell_type": "markdown",
"id": "883f363c-c110-41fa-b64f-f9f2419b6967",
"metadata": {},
"source": [
"You will get better output once you run it from terminal:\n",
"![](./static/scalene_example.png)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ba7fa86-fe7f-4db9-9a5d-66457daa8887",
"metadata": {},
"outputs": [],
"source": [
"%pip install memory-profiler "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b35e7e4d-760d-4339-895c-209cff1fc317",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting example/memory_profile.py\n"
]
}
],
"source": [
"%%writefile example/memory_profile.py\n",
"@profile\n",
"def my_func():\n",
" a = [1] * (10 ** 6)\n",
" b = [2] * (2 * 10 ** 7)\n",
" del b\n",
" return a\n",
"\n",
"my_func()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0e3bc5c2-eb97-47f4-9fa2-060f46d64719",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Filename: example/memory_profile.py\n",
"\n",
"Line # Mem usage Increment Occurrences Line Contents\n",
"=============================================================\n",
" 1 37.766 MiB 37.766 MiB 1 @profile\n",
" 2 def my_func():\n",
" 3 45.410 MiB 7.645 MiB 1 a = [1] * (10 ** 6)\n",
" 4 198.000 MiB 152.590 MiB 1 b = [2] * (2 * 10 ** 7)\n",
" 5 45.414 MiB -152.586 MiB 1 del b\n",
" 6 45.414 MiB 0.000 MiB 1 return a\n",
"\n",
"\n"
]
}
],
"source": [
"!python -m memory_profiler example/memory_profile.py"
]
},
{
"cell_type": "markdown",
"id": "d760cc67-2734-4454-a14a-06f4e46ec29f",
Expand Down Expand Up @@ -1700,7 +1839,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
9 changes: 9 additions & 0 deletions content/example/concate_with_join.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@profile
def add_string_with_join(iters):
l = []
for i in range(iters):
l.append("abc")
s = "".join(l)
assert len(s) == 3*iters

add_string_with_join(50000)
8 changes: 8 additions & 0 deletions content/example/concate_with_plus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@profile
def add_string_with_plus(iters):
s = ""
for i in range(iters):
s += "abc"
assert len(s) == 3*iters

add_string_with_plus(50000)
8 changes: 8 additions & 0 deletions content/example/memory_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@profile
def my_func():
a = [1] * (10 ** 6)
b = [2] * (2 * 10 ** 7)
del b
return a

my_func()
Binary file added content/static/scalene_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 43a4d95

Please sign in to comment.