Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active September 23, 2020 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genkuroki/0622be2284d58b34a3f9a5fca48a45af to your computer and use it in GitHub Desktop.
Save genkuroki/0622be2284d58b34a3f9a5fca48a45af to your computer and use it in GitHub Desktop.
mutable vs. immutable vs. ambiguous
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "VERSION",
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 1,
"data": {
"text/plain": "v\"1.6.0-DEV.928\""
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "module A\n\nusing Parameters\n\nstruct Param{T}\n dt::T\n ω²::T\nend\n\nmutable struct System{T}\n x::T\n v::T\nend\n\nfunction leapfrog!(par::Param, sys::System)\n @unpack dt, ω² = par\n sys.x += sys.v*dt\n sys.v += -ω²*sys.x*dt\n sys\nend\n\nfunction test1(L=10^6)\n par = Param{Float64}(1e-3, 4)\n sys = System{Float64}(2, 0)\n for i in 1:L\n leapfrog!(par, sys)\n end\n sys\nend\n\nBase.copy(sys::System{T}) where T = System{T}(sys.x, sys.v)\n\nfunction test2(L=10^6)\n par = Param{Float64}(1e-3, 4)\n sys = System{Float64}(2, 0)\n sol = Array{typeof(sys)}(undef, L+1)\n sol[begin] = sys\n for i in 1:L\n leapfrog!(par, sys)\n sol[begin+i] = copy(sys)\n end\n sol\nend\n\nend",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "Main.A"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "module B\n\nusing Parameters\n\nstruct Param{T}\n dt::T\n ω²::T\nend\n\nstruct System{T}\n x::T\n v::T\nend\n\nfunction leapfrog(par::Param, sys::System)\n @unpack dt, ω² = par\n x = sys.x + sys.v*dt\n v = sys.v - ω²*x*dt\n System(x, v)\nend\n\nfunction test1(L=10^6)\n par = Param{Float64}(1e-3, 4)\n sys = System{Float64}(2, 0)\n for i in 1:L\n sys = leapfrog(par, sys)\n end\n sys\nend\n\nfunction test2(L=10^6)\n par = Param{Float64}(1e-3, 4)\n sys = System{Float64}(2, 0)\n sol = Array{typeof(sys)}(undef, L+1)\n sol[begin] = sys\n for i in 1:L\n sol[begin+i] = leapfrog(par, sol[begin+i-1])\n end\n sol\nend\n\nend",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "Main.B"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "module C\n\nusing Parameters\n\nstruct Param\n dt\n ω²\nend\n\nstruct System\n x\n v\nend\n\nfunction leapfrog(par::Param, sys::System)\n @unpack dt, ω² = par\n x = sys.x + sys.v*dt\n v = sys.v - ω²*x*dt\n System(x, v)\nend\n\nfunction test1(L=10^6)\n par = Param(1e-3, 4.0)\n sys = System(2.0, 0.0)\n for i in 1:L\n sys = leapfrog(par, sys)\n end\n sys\nend\n\nfunction test2(L=10^6)\n par = Param(1e-3, 4.0)\n sys = System(2.0, 0.0)\n sol = Vector{typeof(sys)}(undef, L+1)\n sol[begin] = sys\n for i in 1:L\n sol[begin+i] = leapfrog(par, sol[begin+i-1])\n end\n sol\nend\n\nend",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "Main.C"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "module D\n\nusing Parameters\nusing StaticArrays\n\nstruct Param{T}\n dt::T\n ω²::T\nend\n\nfunction leapfrog(par::Param, sys::SVector)\n @unpack dt, ω² = par\n x = sys[1] + sys[2]*dt\n v = sys[2] - ω²*x*dt\n SVector(x, v)\nend\n\nfunction test1(L=10^6)\n par = Param{Float64}(1e-3, 4)\n sys = SVector{2, Float64}(2, 0)\n for i in 1:L\n sys = leapfrog(par, sys)\n end\n sys\nend\n\nfunction test2(L=10^6)\n par = Param{Float64}(1e-3, 4)\n sys = SVector{2, Float64}(2, 0)\n sol = Vector{typeof(sys)}(undef, L+1)\n sol[begin] = sys\n for i in 1:L\n sol[begin+i] = leapfrog(par, sol[begin+i-1])\n end\n sol\nend\n\nend",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "Main.D"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using BenchmarkTools",
"execution_count": 6,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "?SVector",
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": "search: Den\u001b[0m\u001b[1ms\u001b[22me\u001b[0m\u001b[1mV\u001b[22m\u001b[0m\u001b[1me\u001b[22m\u001b[0m\u001b[1mc\u001b[22m\u001b[0m\u001b[1mt\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1mr\u001b[22m Ab\u001b[0m\u001b[1ms\u001b[22mtract\u001b[0m\u001b[1mV\u001b[22m\u001b[0m\u001b[1me\u001b[22m\u001b[0m\u001b[1mc\u001b[22m\u001b[0m\u001b[1mt\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1mr\u001b[22m \u001b[0m\u001b[1mS\u001b[22mtrided\u001b[0m\u001b[1mV\u001b[22m\u001b[0m\u001b[1me\u001b[22m\u001b[0m\u001b[1mc\u001b[22m\u001b[0m\u001b[1mt\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1mr\u001b[22m Den\u001b[0m\u001b[1ms\u001b[22me\u001b[0m\u001b[1mV\u001b[22m\u001b[0m\u001b[1me\u001b[22m\u001b[0m\u001b[1mc\u001b[22mOrMa\u001b[0m\u001b[1mt\u001b[22m Ab\u001b[0m\u001b[1ms\u001b[22mtract\u001b[0m\u001b[1mV\u001b[22m\u001b[0m\u001b[1me\u001b[22m\u001b[0m\u001b[1mc\u001b[22mOrMa\u001b[0m\u001b[1mt\u001b[22m\n\nCouldn't find \u001b[36mSVector\u001b[39m\nPerhaps you meant Vector, BitVector or Set\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": " No documentation found.\n\n Binding \u001b[36mSVector\u001b[39m does not exist.",
"text/markdown": "No documentation found.\n\nBinding `SVector` does not exist.\n",
"text/latex": "No documentation found.\n\nBinding \\texttt{SVector} does not exist.\n\n"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## test 1"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@show A.test1()\n@show B.test1()\n@show C.test1()\n@show D.test1();",
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": "A.test1() = Main.A.System{Float64}(-0.7336792491144193, -3.71966972454744)\nB.test1() = Main.B.System{Float64}(-0.7336792491144193, -3.71966972454744)\nC.test1() = Main.C.System(-0.7336792491144193, -3.71966972454744)\nD.test1() = [-0.7336792491144193, -3.71966972454744]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(\"mutable case:\")\na = @benchmark A.test1()",
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": "mutable case:",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 32 bytes\n allocs estimate: 1\n --------------\n minimum time: 6.378 ms (0.00% GC)\n median time: 6.617 ms (0.00% GC)\n mean time: 6.910 ms (0.00% GC)\n maximum time: 16.789 ms (0.00% GC)\n --------------\n samples: 724\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(\"immutable case:\")\nb = @benchmark B.test1()",
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": "immutable case:",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 0 bytes\n allocs estimate: 0\n --------------\n minimum time: 6.196 ms (0.00% GC)\n median time: 6.542 ms (0.00% GC)\n mean time: 6.562 ms (0.00% GC)\n maximum time: 12.847 ms (0.00% GC)\n --------------\n samples: 762\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(\"ambiguous case:\")\nc = @benchmark C.test1()",
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": "ambiguous case:",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 11,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 61.04 MiB\n allocs estimate: 4000000\n --------------\n minimum time: 112.432 ms (0.94% GC)\n median time: 115.190 ms (1.01% GC)\n mean time: 119.169 ms (1.27% GC)\n maximum time: 159.780 ms (0.87% GC)\n --------------\n samples: 42\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "minimum(c).time/minimum(b).time",
"execution_count": 12,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 12,
"data": {
"text/plain": "18.145920839145536"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(\"static vector case:\")\nd = @benchmark D.test1()",
"execution_count": 13,
"outputs": [
{
"output_type": "stream",
"text": "static vector case:",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 13,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 0 bytes\n allocs estimate: 0\n --------------\n minimum time: 6.378 ms (0.00% GC)\n median time: 6.568 ms (0.00% GC)\n mean time: 6.665 ms (0.00% GC)\n maximum time: 12.986 ms (0.00% GC)\n --------------\n samples: 751\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## test 2"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@show A.test2()[(begin+end)÷2]\n@show B.test2()[(begin+end)÷2]\n@show C.test2()[(begin+end)÷2]\n@show D.test2()[(begin+end)÷2];",
"execution_count": 14,
"outputs": [
{
"output_type": "stream",
"text": "(A.test2())[(begin + end) ÷ 2] = Main.A.System{Float64}(1.1261364576682884, -3.3078946896892893)\n(B.test2())[(begin + end) ÷ 2] = Main.B.System{Float64}(1.1261364576682884, -3.3078946896892893)\n(C.test2())[(begin + end) ÷ 2] = Main.C.System(1.1261364576682884, -3.3078946896892893)\n(D.test2())[(begin + end) ÷ 2] = [1.1261364576682884, -3.3078946896892893]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(\"mutable case:\")\na = @benchmark A.test2()",
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"text": "mutable case:",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 15,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 38.15 MiB\n allocs estimate: 1000003\n --------------\n minimum time: 12.503 ms (0.00% GC)\n median time: 34.127 ms (66.60% GC)\n mean time: 34.431 ms (59.18% GC)\n maximum time: 151.250 ms (86.17% GC)\n --------------\n samples: 145\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(\"immutable case:\")\nb = @benchmark B.test2()",
"execution_count": 16,
"outputs": [
{
"output_type": "stream",
"text": "immutable case:",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 16,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 15.26 MiB\n allocs estimate: 2\n --------------\n minimum time: 9.952 ms (0.00% GC)\n median time: 10.530 ms (0.00% GC)\n mean time: 12.131 ms (14.56% GC)\n maximum time: 84.929 ms (88.15% GC)\n --------------\n samples: 411\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(\"ambiguous case:\")\nc = @benchmark C.test2()",
"execution_count": 17,
"outputs": [
{
"output_type": "stream",
"text": "ambiguous case:",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 17,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 76.29 MiB\n allocs estimate: 4000002\n --------------\n minimum time: 148.510 ms (0.00% GC)\n median time: 220.879 ms (39.01% GC)\n mean time: 229.641 ms (39.43% GC)\n maximum time: 352.187 ms (47.34% GC)\n --------------\n samples: 22\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "minimum(c).time/minimum(b).time",
"execution_count": 18,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 18,
"data": {
"text/plain": "14.92321175256122"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(\"static vector case:\")\nd = @benchmark D.test2()",
"execution_count": 19,
"outputs": [
{
"output_type": "stream",
"text": "static vector case:",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 19,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 15.26 MiB\n allocs estimate: 2\n --------------\n minimum time: 11.930 ms (0.00% GC)\n median time: 13.056 ms (0.00% GC)\n mean time: 14.710 ms (12.71% GC)\n maximum time: 88.543 ms (86.07% GC)\n --------------\n samples: 340\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"kernelspec": {
"name": "julia-1.6-o3-depwarn",
"display_name": "Julia 1.6.0-DEV depwarn -O3",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.6.0"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"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": "0622be2284d58b34a3f9a5fca48a45af",
"data": {
"description": "mutable vs. immutable vs. ambiguous",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/0622be2284d58b34a3f9a5fca48a45af"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment