Skip to content

Commit ea60491

Browse files
authored
Integers
1 parent bf1ddc4 commit ea60491

File tree

1 file changed

+273
-0
lines changed

1 file changed

+273
-0
lines changed

Integers.ipynb

+273
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "2fcc44e4",
6+
"metadata": {},
7+
"source": [
8+
"### Integers are objects - instances of the int class."
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 1,
14+
"id": "58bfe28e",
15+
"metadata": {},
16+
"outputs": [
17+
{
18+
"name": "stdout",
19+
"output_type": "stream",
20+
"text": [
21+
"<class 'int'>\n"
22+
]
23+
}
24+
],
25+
"source": [
26+
"print(type(100))"
27+
]
28+
},
29+
{
30+
"cell_type": "markdown",
31+
"id": "d8804491",
32+
"metadata": {},
33+
"source": [
34+
"They are a variable length data type that can theoretically handle any integer magnitude. This will take up a variable amount of memory that depends on the particular size of the integer."
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": 2,
40+
"id": "a57cbc3b",
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"import sys"
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"id": "da285253",
50+
"metadata": {},
51+
"source": [
52+
"### Creating an integer object requires an overhead of 24 bytes:"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 3,
58+
"id": "dcbe7547",
59+
"metadata": {},
60+
"outputs": [
61+
{
62+
"data": {
63+
"text/plain": [
64+
"24"
65+
]
66+
},
67+
"execution_count": 3,
68+
"metadata": {},
69+
"output_type": "execute_result"
70+
}
71+
],
72+
"source": [
73+
"sys.getsizeof(0)"
74+
]
75+
},
76+
{
77+
"cell_type": "markdown",
78+
"id": "bc5d5dd4",
79+
"metadata": {},
80+
"source": [
81+
"### Here we see that to store the number 1 required 4 bytes (32 bits) on top of the 24 byte overhead."
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 4,
87+
"id": "5b299861",
88+
"metadata": {},
89+
"outputs": [
90+
{
91+
"data": {
92+
"text/plain": [
93+
"28"
94+
]
95+
},
96+
"execution_count": 4,
97+
"metadata": {},
98+
"output_type": "execute_result"
99+
}
100+
],
101+
"source": [
102+
"sys.getsizeof(1)"
103+
]
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"id": "75a16990",
108+
"metadata": {},
109+
"source": [
110+
"Larger numbers will require more storage space."
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": 6,
116+
"id": "732d72fc",
117+
"metadata": {},
118+
"outputs": [
119+
{
120+
"data": {
121+
"text/plain": [
122+
"160"
123+
]
124+
},
125+
"execution_count": 6,
126+
"metadata": {},
127+
"output_type": "execute_result"
128+
}
129+
],
130+
"source": [
131+
"sys.getsizeof(2**1000)"
132+
]
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"id": "079cfe0f",
137+
"metadata": {},
138+
"source": [
139+
"### Larger integers will also slow down calculations."
140+
]
141+
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": 9,
145+
"id": "546f4497",
146+
"metadata": {},
147+
"outputs": [],
148+
"source": [
149+
"import time"
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": 10,
155+
"id": "4918c5d7",
156+
"metadata": {},
157+
"outputs": [],
158+
"source": [
159+
"def calc(a):\n",
160+
" for i in range(10000000):\n",
161+
" a * 2"
162+
]
163+
},
164+
{
165+
"cell_type": "markdown",
166+
"id": "a3f02850",
167+
"metadata": {},
168+
"source": [
169+
"We start with a small integer value for a (10):"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": 12,
175+
"id": "5bdc40d4",
176+
"metadata": {},
177+
"outputs": [
178+
{
179+
"name": "stdout",
180+
"output_type": "stream",
181+
"text": [
182+
"0.8892209999999636\n"
183+
]
184+
}
185+
],
186+
"source": [
187+
"start = time.perf_counter()\n",
188+
"calc(10)\n",
189+
"end = time.perf_counter()\n",
190+
"print(end - start)"
191+
]
192+
},
193+
{
194+
"cell_type": "markdown",
195+
"id": "cf3ba121",
196+
"metadata": {},
197+
"source": [
198+
"### Now we set a to something larger (2^100)):"
199+
]
200+
},
201+
{
202+
"cell_type": "code",
203+
"execution_count": 13,
204+
"id": "efd8a237",
205+
"metadata": {},
206+
"outputs": [
207+
{
208+
"name": "stdout",
209+
"output_type": "stream",
210+
"text": [
211+
"1.5697594000000095\n"
212+
]
213+
}
214+
],
215+
"source": [
216+
"start = time.perf_counter()\n",
217+
"calc(2**100)\n",
218+
"end = time.perf_counter()\n",
219+
"print(end - start)"
220+
]
221+
},
222+
{
223+
"cell_type": "markdown",
224+
"id": "1eda1abc",
225+
"metadata": {},
226+
"source": [
227+
"### Finally we set a to some really large value (2^10,000)"
228+
]
229+
},
230+
{
231+
"cell_type": "code",
232+
"execution_count": 14,
233+
"id": "5928ae07",
234+
"metadata": {},
235+
"outputs": [
236+
{
237+
"name": "stdout",
238+
"output_type": "stream",
239+
"text": [
240+
"8.066712399999972\n"
241+
]
242+
}
243+
],
244+
"source": [
245+
"start = time.perf_counter()\n",
246+
"calc(2**10000)\n",
247+
"end = time.perf_counter()\n",
248+
"print(end - start)"
249+
]
250+
}
251+
],
252+
"metadata": {
253+
"kernelspec": {
254+
"display_name": "Python 3 (ipykernel)",
255+
"language": "python",
256+
"name": "python3"
257+
},
258+
"language_info": {
259+
"codemirror_mode": {
260+
"name": "ipython",
261+
"version": 3
262+
},
263+
"file_extension": ".py",
264+
"mimetype": "text/x-python",
265+
"name": "python",
266+
"nbconvert_exporter": "python",
267+
"pygments_lexer": "ipython3",
268+
"version": "3.9.12"
269+
}
270+
},
271+
"nbformat": 4,
272+
"nbformat_minor": 5
273+
}

0 commit comments

Comments
 (0)