Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save genkuroki/b8003ce3f2800c3d81b40ed913ffeb0f to your computer and use it in GitHub Desktop.
Save genkuroki/b8003ce3f2800c3d81b40ed913ffeb0f to your computer and use it in GitHub Desktop.
Jupyter上でのforループのスコープ
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# Jupyter上でのforループのスコープ\n\n結論: `global` を付けなくても「期待」通りの結果が得られるが, forループをマクロの中に入れてしまうと通用しなくなるので, 函数外では `global` を付けて書くべきであることを知っておいた方がよい."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "n = 10^7\nc = 0\nfor i in 1:n\n c += ifelse(rand()^2 + rand()^2 < 1, 1, 0)\nend\n4c/n",
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 1,
"data": {
"text/plain": "3.1415296"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "n = 10^7\nc = 0\n@time for i in 1:n\n c += ifelse(rand()^2 + rand()^2 < 1, 1, 0)\nend\n4c/n",
"execution_count": 2,
"outputs": [
{
"output_type": "error",
"ename": "UndefVarError",
"evalue": "UndefVarError: c not defined",
"traceback": [
"UndefVarError: c not defined",
"",
"Stacktrace:",
" [1] macro expansion at .\\In[2]:4 [inlined]",
" [2] macro expansion at .\\util.jl:156 [inlined]",
" [3] top-level scope at .\\In[2]:3 [inlined]",
" [4] top-level scope at .\\none:0"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "n = 10^7\nc = 0\nfor i in 1:n\n global c += ifelse(rand()^2 + rand()^2 < 1, 1, 0)\nend\n4c/n",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "3.1414956"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "n = 10^7\nc = 0\n@time for i in 1:n\n global c += ifelse(rand()^2 + rand()^2 < 1, 1, 0)\nend\n4c/n",
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": " 1.245922 seconds (40.00 M allocations: 762.914 MiB, 3.81% gc time)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "3.1421164"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function pi_MonteCarlo(n=10^7)\n c = 0\n for i in 1:n\n c += ifelse(rand()^2 + rand()^2 < 1, 1, 0)\n end\n 4c/n\nend\npi_MonteCarlo(1);",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@time pi_MonteCarlo()",
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": " 0.081078 seconds (35.84 k allocations: 1.959 MiB)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "3.1409988"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@time pi_MonteCarlo(10^9)",
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": " 3.555929 seconds (6 allocations: 192 bytes)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "3.141561348"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"kernelspec": {
"name": "julia-1.1",
"display_name": "Julia 1.1.1",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.1.1"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": true,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"gist": {
"id": "b8003ce3f2800c3d81b40ed913ffeb0f",
"data": {
"description": "Jupyter上でのforループのスコープ",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/b8003ce3f2800c3d81b40ed913ffeb0f"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment