Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active February 12, 2018 03:07
Show Gist options
  • Save genkuroki/44e62dfa12e5fcdc93296501138c8267 to your computer and use it in GitHub Desktop.
Save genkuroki/44e62dfa12e5fcdc93296501138c8267 to your computer and use it in GitHub Desktop.
色々なsum
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# 色々なsum\n\n黒木玄\n\n2018-01-05"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using BenchmarkTools",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# sum(rand(N)) はメモリを大量消費\n\nsrand(2018)\na(N) = sum(rand(N))\n@show a(10^8)\nsleep(0.1)\n@benchmark a(10^8)",
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": "a(10 ^ 8) = 4.999878401818617e7\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 762.94 MiB\n allocs estimate: 2\n --------------\n minimum time: 323.326 ms (2.13% GC)\n median time: 373.239 ms (14.14% GC)\n mean time: 378.576 ms (15.26% GC)\n maximum time: 431.282 ms (26.81% GC)\n --------------\n samples: 14\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# sum(rand() for i in 1:N) はメモリは節約するが sum(rand(N)) より遅い.\n\nsrand(2018)\nb(N) = sum(rand() for i in 1:N)\n@show b(10^8)\nsleep(0.1)\n@benchmark b(10^8)",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": "b(10 ^ 8) = 4.999878401820758e7\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 64 bytes\n allocs estimate: 3\n --------------\n minimum time: 635.041 ms (0.00% GC)\n median time: 648.737 ms (0.00% GC)\n mean time: 652.902 ms (0.00% GC)\n maximum time: 684.673 ms (0.00% GC)\n --------------\n samples: 8\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# forループはメモリも消費ぜず速い\n\nsrand(2018)\nc(N) = (s = 0.0; for i in 1:N s += rand() end; s)\n@show c(10^8)\nsleep(0.1)\n@benchmark c(10^8)",
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": "c(10 ^ 8) = 4.999878401820758e7\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 0 bytes\n allocs estimate: 0\n --------------\n minimum time: 176.926 ms (0.00% GC)\n median time: 178.316 ms (0.00% GC)\n mean time: 178.863 ms (0.00% GC)\n maximum time: 181.458 ms (0.00% GC)\n --------------\n samples: 28\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# forループをマクロにすると @sum(0.0, _, 1:N, rand()) と短く書ける\n\nmacro sum(init, i, iter, f)\n quote\n begin\n s = $(esc(init))\n for $(esc(i)) in $(esc(iter))\n s += $(esc(f))\n end\n s\n end\n end\nend\n\nsrand(2018)\nc_macro(N) = @sum(0.0, _, 1:N, rand())\n@show c_macro(10^8)\nsleep(0.1)\n@benchmark c_macro(10^8)",
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": "c_macro(10 ^ 8) = 4.999878401820758e7\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 0 bytes\n allocs estimate: 0\n --------------\n minimum time: 177.346 ms (0.00% GC)\n median time: 179.926 ms (0.00% GC)\n mean time: 181.864 ms (0.00% GC)\n maximum time: 203.309 ms (0.00% GC)\n --------------\n samples: 28\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@show @sum(0, i, 1:10, @sum(0, j, 1:10, i*j))\n@show sum(sum(i*j for j in 1:10) for i in 1:10)\n\n@macroexpand @sum(0, i, 1:10, @sum(0, j, 1:10, i*j))",
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": "@sum(0, i, 1:10, @sum(0, j, 1:10, i * j)) = 3025\nsum((sum((i * j for j = 1:10)) for i = 1:10)) = 3025\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "quote # In[5], line 5:\n begin # In[5], line 6:\n #20#s = 0 # In[5], line 7:\n for i = 1:10 # In[5], line 8:\n #20#s += begin # In[5], line 5:\n begin # In[5], line 6:\n #21#s = 0 # In[5], line 7:\n for j = 1:10 # In[5], line 8:\n #21#s += i * j\n end # In[5], line 10:\n #21#s\n end\n end\n end # In[5], line 10:\n #20#s\n end\nend"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# 並列処理するとさらに速くなる\n\nsrand(2018)\nd(N) = @parallel (+) for i in 1:N rand() end\n@show d(10^8)\nsleep(0.1)\n@benchmark d(10^8)",
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": "d(10 ^ 8) = 5.000092105716484e7\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 88.80 KiB\n allocs estimate: 954\n --------------\n minimum time: 59.624 ms (0.00% GC)\n median time: 61.321 ms (0.00% GC)\n mean time: 62.525 ms (0.00% GC)\n maximum time: 82.463 ms (0.00% GC)\n --------------\n samples: 80\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# 並列処理で和を計算するマクロ\n\nmacro sum_parallel(i, iter, f)\n quote\n @parallel (+) for $(esc(i)) in $(esc(iter)); $(esc(f)); end\n end\nend\n\nsrand(2018)\nd_macro(N) = @sum_parallel(_, 1:N, rand())\n@show d_macro(10^8)\nsleep(0.1)\n@benchmark d_macro(10^8)",
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": "d_macro(10 ^ 8) = 5.0003829685399316e7\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 88.45 KiB\n allocs estimate: 947\n --------------\n minimum time: 58.549 ms (0.00% GC)\n median time: 61.295 ms (0.00% GC)\n mean time: 61.791 ms (0.00% GC)\n maximum time: 69.428 ms (0.00% GC)\n --------------\n samples: 82\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@show @sum_parallel(i, 1:10, @sum_parallel(j, 1:10, i*j))\n@show sum(sum(i*j for j in 1:10) for i in 1:10);",
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": "@sum_parallel(i, 1:10, @sum_parallel(j, 1:10, i * j)) = 3025\nsum((sum((i * j for j = 1:10)) for i = 1:10)) = 3025\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"_draft": {
"nbviewer_url": "https://gist.github.com/44e62dfa12e5fcdc93296501138c8267"
},
"gist": {
"id": "44e62dfa12e5fcdc93296501138c8267",
"data": {
"description": "色々なsum",
"public": true
}
},
"kernelspec": {
"name": "julia-0.6-pauto",
"display_name": "Julia 0.6.2 procs auto",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "0.6.2"
},
"toc": {
"threshold": 4,
"number_sections": true,
"toc_cell": false,
"toc_window_display": false,
"toc_section_display": "block",
"sideBar": true,
"navigate_menu": true,
"moveMenuLeft": true,
"widenNotebook": false,
"colors": {
"hover_highlight": "#DAA520",
"selected_highlight": "#FFD700",
"running_highlight": "#FF0000",
"wrapper_background": "#FFFFFF",
"sidebar_border": "#EEEEEE",
"navigate_text": "#333333",
"navigate_num": "#000000"
},
"nav_menu": {
"height": "31px",
"width": "252px"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment