Skip to content

Commit 63271f0

Browse files
Vipul-Cariappamcbarton
authored andcommitted
add example notebook
1 parent bd51e03 commit 63271f0

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed

notebooks/sub-interpreters.ipynb

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "fb74181a-b881-4ae9-9224-495425707762",
6+
"metadata": {},
7+
"source": [
8+
"**Default Interpreter of the Kernel**"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 1,
14+
"id": "e5375e1a-347b-461e-97ac-1a5bc708c896",
15+
"metadata": {
16+
"vscode": {
17+
"languageId": "c++"
18+
}
19+
},
20+
"outputs": [
21+
{
22+
"name": "stdout",
23+
"output_type": "stream",
24+
"text": [
25+
"201703\n"
26+
]
27+
}
28+
],
29+
"source": [
30+
"#include <iostream>\n",
31+
"std::cout << __cplusplus << std::endl;"
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"id": "7ec1684f-ce9a-4614-8f7c-8391b68f7eb5",
37+
"metadata": {},
38+
"source": [
39+
"**Creating New Sub-Interpreter with `-std=c++23`**\n",
40+
"\n",
41+
"_We give it a name `cpp23` to re-use later_"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 2,
47+
"id": "756696a7-c8db-4302-8794-029664673952",
48+
"metadata": {
49+
"vscode": {
50+
"languageId": "c++"
51+
}
52+
},
53+
"outputs": [
54+
{
55+
"name": "stdout",
56+
"output_type": "stream",
57+
"text": [
58+
"Elapsed time (C++23): 5.97e-06 seconds\n"
59+
]
60+
}
61+
],
62+
"source": [
63+
"%%subinterp -Wall -O3 --std=c++23 --name cpp23\n",
64+
"\n",
65+
"#include <iostream>\n",
66+
"#include <vector>\n",
67+
"#include <ranges>\n",
68+
"#include <chrono>\n",
69+
"\n",
70+
"void version() { std::cout << __cplusplus << std::endl; }\n",
71+
"\n",
72+
"template <std::ranges::range R> constexpr auto to_vector(R&& r) { return std::vector<std::decay_t<std::ranges::range_value_t<R>>>{r.begin(), r.end()}; }\n",
73+
"\n",
74+
"void run() {\n",
75+
" std::vector<int> numbers(10'000);\n",
76+
" for (int i = 0; i < 10'000; i++) numbers.push_back(i);\n",
77+
" \n",
78+
" auto start = std::chrono::high_resolution_clock::now();\n",
79+
" std::vector<int> result = to_vector(\n",
80+
" numbers \n",
81+
" | std::views::filter([](int n) { return n % 2 != 0; }) // filter out even numbers\n",
82+
" | std::views::transform([](int n) { return n * 2; })); // multiply remaining by 2\n",
83+
" auto end = std::chrono::high_resolution_clock::now();\n",
84+
" \n",
85+
" std::chrono::duration<double> elapsed = end - start;\n",
86+
" std::cout << \"Elapsed time (C++23): \" << elapsed.count() << \" seconds\\n\";\n",
87+
"}\n",
88+
"run();"
89+
]
90+
},
91+
{
92+
"cell_type": "markdown",
93+
"id": "1a31f103-b92a-44e9-bf42-61709340bdc4",
94+
"metadata": {},
95+
"source": [
96+
"**Creating Another Sub-Interpreter with `-std=c++11`**"
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": 3,
102+
"id": "4f3391f7-0a38-4cd9-af2b-33245a06db84",
103+
"metadata": {
104+
"vscode": {
105+
"languageId": "c++"
106+
}
107+
},
108+
"outputs": [
109+
{
110+
"name": "stdout",
111+
"output_type": "stream",
112+
"text": [
113+
"Elapsed time (C++11): 5.22e-06 seconds\n"
114+
]
115+
}
116+
],
117+
"source": [
118+
"%%subinterp -O3 --std=c++11 --name cpp11\n",
119+
"\n",
120+
"#include <iostream>\n",
121+
"#include <vector>\n",
122+
"#include <algorithm>\n",
123+
"#include <chrono>\n",
124+
"\n",
125+
"void version() { std::cout << __cplusplus << std::endl; }\n",
126+
"\n",
127+
"void run() {\n",
128+
" std::vector<int> numbers(10000);\n",
129+
" for (int i = 0; i < 10000; i++) numbers.push_back(i);\n",
130+
" std::vector<int> result;\n",
131+
"\n",
132+
" auto start = std::chrono::high_resolution_clock::now();\n",
133+
" std::copy_if(numbers.begin(), numbers.end(), std::back_inserter(result),\n",
134+
" [](int n) { return n % 2 != 0; }); // filter out even numbers\n",
135+
" std::transform(result.begin(), result.end(), result.begin(),\n",
136+
" [](int n) { return n * 2; }); // multiply remaining by 2\n",
137+
" auto end = std::chrono::high_resolution_clock::now();\n",
138+
" \n",
139+
" std::chrono::duration<double> elapsed = end - start;\n",
140+
" std::cout << \"Elapsed time (C++11): \" << elapsed.count() << \" seconds\\n\";\n",
141+
"}\n",
142+
"run();"
143+
]
144+
},
145+
{
146+
"cell_type": "markdown",
147+
"id": "63cecedc-0e72-43fd-80e4-36a612a8042b",
148+
"metadata": {},
149+
"source": [
150+
"**Re-Using the Interpreter Created Previously**"
151+
]
152+
},
153+
{
154+
"cell_type": "code",
155+
"execution_count": 4,
156+
"id": "600da197-f902-4cf4-a401-a848e08aa638",
157+
"metadata": {
158+
"vscode": {
159+
"languageId": "c++"
160+
}
161+
},
162+
"outputs": [
163+
{
164+
"name": "stdout",
165+
"output_type": "stream",
166+
"text": [
167+
"202302\n"
168+
]
169+
}
170+
],
171+
"source": [
172+
"%%subinterp --use cpp23\n",
173+
"version();"
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": 5,
179+
"id": "2c2fa915-a6de-4477-b4f8-6c342720c224",
180+
"metadata": {
181+
"vscode": {
182+
"languageId": "c++"
183+
}
184+
},
185+
"outputs": [
186+
{
187+
"name": "stdout",
188+
"output_type": "stream",
189+
"text": [
190+
"201103\n"
191+
]
192+
}
193+
],
194+
"source": [
195+
"%%subinterp --use cpp11\n",
196+
"version();"
197+
]
198+
}
199+
],
200+
"metadata": {
201+
"kernelspec": {
202+
"display_name": "C++17",
203+
"language": "cpp",
204+
"name": "xcpp17"
205+
},
206+
"language_info": {
207+
"codemirror_mode": "text/x-c++src",
208+
"file_extension": ".cpp",
209+
"mimetype": "text/x-c++src",
210+
"name": "C++",
211+
"version": "17"
212+
}
213+
},
214+
"nbformat": 4,
215+
"nbformat_minor": 5
216+
}

0 commit comments

Comments
 (0)