Created
May 27, 2021 16:55
-
-
Save genkuroki/b8a1b1b601ec38a3aebee4588dc28349 to your computer and use it in GitHub Desktop.
Traceur example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "https://github.com/JunoLab/Traceur.jl" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "using Traceur\n\nfunction f()\n y = 1.0\n for i in 1:n\n y += x*y/n\n end\n y\nend\n\nx = 1.0\nn = 10^6\n\n@time f()\n@time f()\n@time f()", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": " 0.152444 seconds (6.00 M allocations: 106.799 MiB, 11.84% gc time, 3.50% compilation time)\n 0.125421 seconds (6.00 M allocations: 106.796 MiB, 11.61% gc time)\n 0.117651 seconds (6.00 M allocations: 106.796 MiB, 5.15% gc time)\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 1, | |
"data": { | |
"text/plain": "2.7182804693194718" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "n = 10\n@trace f()", | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "┌ Warning: uses global variable Main.n\n└ @ In[1]:5\n┌ Warning: uses global variable Main.x\n└ @ In[1]:6\n┌ Warning: uses global variable Main.n\n└ @ In[1]:6\n┌ Warning: y is assigned as Float64\n└ @ In[1]:4\n┌ Warning: y is assigned as Any\n└ @ In[1]:6\n┌ Warning: dynamic dispatch to 1:Main.n\n└ @ In[1]:5\n┌ Warning: dynamic dispatch to Base.iterate(φ (%9 => %8, %11 => %10))\n└ @ In[1]:5\n┌ Warning: dynamic dispatch to Main.x * φ (%16 => 1.0, %30 => %25)\n└ @ In[1]:6\n┌ Warning: dynamic dispatch to (Main.x * φ (%16 => 1.0, %30 => %25)) / Main.n\n└ @ In[1]:6\n┌ Warning: dynamic dispatch to φ (%16 => 1.0, %30 => %25) + (Main.x * φ (%16 => 1.0, %30 => %25)) / Main.n\n└ @ In[1]:6\n┌ Warning: dynamic dispatch to Base.iterate(φ (%9 => %8, %11 => %10), Core.getfield(φ (%16 => %13, %30 => %26), 2))\n└ @ In[1]:6\n┌ Warning: f returns Any\n└ @ In[1]:3\n", | |
"name": "stderr" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 2, | |
"data": { | |
"text/plain": "2.5937424601" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@code_warntype f()", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Variables\n #self#\u001b[36m::Core.Const(f)\u001b[39m\n @_2\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n y\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n i\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n\nBody\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n\u001b[90m1 ─\u001b[39m (y = 1.0)\n\u001b[90m│ \u001b[39m %2 = (1:Main.n)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n\u001b[90m│ \u001b[39m (@_2 = Base.iterate(%2))\n\u001b[90m│ \u001b[39m %4 = (@_2 === nothing)\u001b[36m::Bool\u001b[39m\n\u001b[90m│ \u001b[39m %5 = Base.not_int(%4)\u001b[36m::Bool\u001b[39m\n\u001b[90m└──\u001b[39m goto #4 if not %5\n\u001b[90m2 ┄\u001b[39m %7 = @_2\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n\u001b[90m│ \u001b[39m (i = Core.getfield(%7, 1))\n\u001b[90m│ \u001b[39m %9 = Core.getfield(%7, 2)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n\u001b[90m│ \u001b[39m %10 = y\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n\u001b[90m│ \u001b[39m %11 = (Main.x * y)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n\u001b[90m│ \u001b[39m %12 = (%11 / Main.n)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n\u001b[90m│ \u001b[39m (y = %10 + %12)\n\u001b[90m│ \u001b[39m (@_2 = Base.iterate(%2, %9))\n\u001b[90m│ \u001b[39m %15 = (@_2 === nothing)\u001b[36m::Bool\u001b[39m\n\u001b[90m│ \u001b[39m %16 = Base.not_int(%15)\u001b[36m::Bool\u001b[39m\n\u001b[90m└──\u001b[39m goto #4 if not %16\n\u001b[90m3 ─\u001b[39m goto #2\n\u001b[90m4 ┄\u001b[39m return y\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function g(x, n)\n y = 1\n for i in 1:n\n y += x*y/n\n end\n y\nend\n\n@trace g(1.0, 100)", | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "┌ Warning: is assigned as Union{Nothing, Tuple{Int64, Int64}}\n└ @ In[4]:3\n┌ Warning: is assigned as Union{Nothing, Tuple{Int64, Int64}}\n└ @ In[4]:4\n┌ Warning: y is assigned as Int64\n└ @ In[4]:2\n┌ Warning: y is assigned as Float64\n└ @ In[4]:4\n┌ Warning: g returns Union{Float64, Int64}\n└ @ In[4]:1\n", | |
"name": "stderr" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 4, | |
"data": { | |
"text/plain": "2.704813829421526" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@code_warntype g(x, n)", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Variables\n #self#\u001b[36m::Core.Const(g)\u001b[39m\n x\u001b[36m::Float64\u001b[39m\n n\u001b[36m::Int64\u001b[39m\n @_4\u001b[33m\u001b[1m::Union{Nothing, Tuple{Int64, Int64}}\u001b[22m\u001b[39m\n y\u001b[91m\u001b[1m::Union{Float64, Int64}\u001b[22m\u001b[39m\n i\u001b[36m::Int64\u001b[39m\n\nBody\u001b[91m\u001b[1m::Union{Float64, Int64}\u001b[22m\u001b[39m\n\u001b[90m1 ─\u001b[39m (y = 1)\n\u001b[90m│ \u001b[39m %2 = (1:n)\u001b[36m::Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64])\u001b[39m\n\u001b[90m│ \u001b[39m (@_4 = Base.iterate(%2))\n\u001b[90m│ \u001b[39m %4 = (@_4 === nothing)\u001b[36m::Bool\u001b[39m\n\u001b[90m│ \u001b[39m %5 = Base.not_int(%4)\u001b[36m::Bool\u001b[39m\n\u001b[90m└──\u001b[39m goto #4 if not %5\n\u001b[90m2 ┄\u001b[39m %7 = @_4::Tuple{Int64, Int64}\u001b[36m::Tuple{Int64, Int64}\u001b[39m\n\u001b[90m│ \u001b[39m (i = Core.getfield(%7, 1))\n\u001b[90m│ \u001b[39m %9 = Core.getfield(%7, 2)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %10 = y\u001b[91m\u001b[1m::Union{Float64, Int64}\u001b[22m\u001b[39m\n\u001b[90m│ \u001b[39m %11 = (x * y)\u001b[36m::Float64\u001b[39m\n\u001b[90m│ \u001b[39m %12 = (%11 / n)\u001b[36m::Float64\u001b[39m\n\u001b[90m│ \u001b[39m (y = %10 + %12)\n\u001b[90m│ \u001b[39m (@_4 = Base.iterate(%2, %9))\n\u001b[90m│ \u001b[39m %15 = (@_4 === nothing)\u001b[36m::Bool\u001b[39m\n\u001b[90m│ \u001b[39m %16 = Base.not_int(%15)\u001b[36m::Bool\u001b[39m\n\u001b[90m└──\u001b[39m goto #4 if not %16\n\u001b[90m3 ─\u001b[39m goto #2\n\u001b[90m4 ┄\u001b[39m return y\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "julia-1.6-depwarn-o3", | |
"display_name": "Julia 1.6.1 depwarn -O3", | |
"language": "julia" | |
}, | |
"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 | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"name": "julia", | |
"mimetype": "application/julia", | |
"version": "1.6.1" | |
}, | |
"@webio": { | |
"lastKernelId": null, | |
"lastCommId": null | |
}, | |
"gist": { | |
"id": "b8a1b1b601ec38a3aebee4588dc28349", | |
"data": { | |
"description": "Traceur example", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/b8a1b1b601ec38a3aebee4588dc28349" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment