Skip to content

Commit 79cf288

Browse files
committed
Compare the runtime
1 parent f9d76bb commit 79cf288

3 files changed

Lines changed: 2090 additions & 24 deletions

File tree

lectures/markov_asset.ipynb

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
{
44
"cell_type": "markdown",
55
"metadata": {
6-
"id": "view-in-github",
7-
"colab_type": "text"
6+
"colab_type": "text",
7+
"id": "view-in-github"
88
},
99
"source": [
1010
"<a href=\"https://colab.research.google.com/github/QuantEcon/lecture-python.myst/blob/update_markov_asset/lectures/markov_asset.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
1111
]
1212
},
1313
{
1414
"cell_type": "code",
15-
"execution_count": 2,
15+
"execution_count": 12,
1616
"id": "ce7fce98",
1717
"metadata": {
1818
"id": "ce7fce98"
@@ -30,7 +30,25 @@
3030
},
3131
{
3232
"cell_type": "code",
33-
"execution_count": 3,
33+
"execution_count": 13,
34+
"id": "50d12e94",
35+
"metadata": {},
36+
"outputs": [
37+
{
38+
"name": "stdout",
39+
"output_type": "stream",
40+
"text": [
41+
"[CpuDevice(id=0)]\n"
42+
]
43+
}
44+
],
45+
"source": [
46+
"print(jax.devices())"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 14,
3452
"id": "b7bcf31f",
3553
"metadata": {
3654
"id": "b7bcf31f"
@@ -135,7 +153,7 @@
135153
},
136154
{
137155
"cell_type": "code",
138-
"execution_count": 4,
156+
"execution_count": 15,
139157
"id": "29a2f7a6",
140158
"metadata": {
141159
"id": "29a2f7a6"
@@ -180,7 +198,7 @@
180198
},
181199
{
182200
"cell_type": "code",
183-
"execution_count": 5,
201+
"execution_count": 16,
184202
"id": "8cfa0f41",
185203
"metadata": {
186204
"id": "8cfa0f41"
@@ -220,7 +238,6 @@
220238
"\n",
221239
" # Compute option price\n",
222240
" p = consol_price(ap, ζ)\n",
223-
" err.throw()\n",
224241
" n = M.shape[0]\n",
225242
" w = jnp.zeros(n)\n",
226243
" error = ϵ + 1\n",
@@ -247,37 +264,78 @@
247264
},
248265
{
249266
"cell_type": "code",
250-
"execution_count": 6,
267+
"execution_count": 17,
251268
"id": "7179ea30",
252269
"metadata": {
270+
"id": "7179ea30",
253271
"mystnb": {
254272
"figure": {
255273
"caption": "Consol price and call option value\n",
256274
"name": "fig_consol_call"
257275
}
258-
},
259-
"id": "7179ea30"
276+
}
260277
},
261278
"outputs": [],
262279
"source": [
263280
"ap = create_ap_model(β=0.9)\n",
264281
"ζ = 1.0\n",
265282
"strike_price = 40\n",
266-
"\n",
267283
"x = ap.mc.state_values\n",
268-
"err, p = consol_price_jit(ap, ζ)\n",
269-
"err.throw()\n",
270-
"err, w = call_option_jit(ap, ζ, strike_price)\n",
271-
"err.throw()\n"
284+
"\n",
285+
"def timer_function():\n",
286+
" err, p = consol_price_jit(ap, ζ)\n",
287+
" err, w = call_option_jit(ap, ζ, strike_price)\n",
288+
"\n",
289+
"result = qe.timeit(\n",
290+
" timer_function, runs=1000, verbose=False, \n",
291+
" results=True, unit=\"milliseconds\"\n",
292+
" )"
293+
]
294+
},
295+
{
296+
"cell_type": "code",
297+
"execution_count": 18,
298+
"id": "1ec35c54",
299+
"metadata": {},
300+
"outputs": [
301+
{
302+
"name": "stdout",
303+
"output_type": "stream",
304+
"text": [
305+
"Results saved to timer_results_jax_cpu.json\n"
306+
]
307+
}
308+
],
309+
"source": [
310+
"import json\n",
311+
"\n",
312+
"# result is already a dictionary, no need to call _asdict()\n",
313+
"result_dict = result\n",
314+
"\n",
315+
"# Define the filename\n",
316+
"filename = 'timer_results_jax_cpu.json'\n",
317+
"\n",
318+
"# Save the dictionary to a JSON file\n",
319+
"with open(filename, 'w') as f:\n",
320+
" json.dump(result_dict, f, indent=4)\n",
321+
"\n",
322+
"print(f\"Results saved to {filename}\")"
272323
]
273324
}
274325
],
275326
"metadata": {
327+
"accelerator": "GPU",
328+
"colab": {
329+
"gpuType": "T4",
330+
"include_colab_link": true,
331+
"provenance": []
332+
},
276333
"jupytext": {
277334
"default_lexer": "ipython"
278335
},
279336
"kernelspec": {
280-
"display_name": "Python 3",
337+
"display_name": "quantecon",
338+
"language": "python",
281339
"name": "python3"
282340
},
283341
"language_info": {
@@ -291,14 +349,8 @@
291349
"nbconvert_exporter": "python",
292350
"pygments_lexer": "ipython3",
293351
"version": "3.13.5"
294-
},
295-
"colab": {
296-
"provenance": [],
297-
"gpuType": "T4",
298-
"include_colab_link": true
299-
},
300-
"accelerator": "GPU"
352+
}
301353
},
302354
"nbformat": 4,
303355
"nbformat_minor": 5
304-
}
356+
}

0 commit comments

Comments
 (0)