Skip to content

Instantly share code, notes, and snippets.

@ceptreee
Created March 25, 2020 19:26
Show Gist options
  • Save ceptreee/0c3031fd88b529286459b56622c06ee8 to your computer and use it in GitHub Desktop.
Save ceptreee/0c3031fd88b529286459b56622c06ee8 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Variables\n",
" #self#\u001b[36m::Core.Compiler.Const(F, false)\u001b[39m\n",
" f\u001b[36m::Array{Function,1}\u001b[39m\n",
" N\u001b[36m::Int64\u001b[39m\n",
" S\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n",
" @_5\u001b[33m\u001b[1m::Union{Nothing, Tuple{Int64,Int64}}\u001b[22m\u001b[39m\n",
" i\u001b[36m::Int64\u001b[39m\n",
"\n",
"Body\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n",
"\u001b[90m1 ─\u001b[39m (N = Main.length(f))\n",
"\u001b[90m│ \u001b[39m (S = 0.0)\n",
"\u001b[90m│ \u001b[39m %3 = (1:N)\u001b[36m::Core.Compiler.PartialStruct(UnitRange{Int64}, Any[Core.Compiler.Const(1, false), Int64])\u001b[39m\n",
"\u001b[90m│ \u001b[39m (@_5 = Base.iterate(%3))\n",
"\u001b[90m│ \u001b[39m %5 = (@_5 === nothing)\u001b[36m::Bool\u001b[39m\n",
"\u001b[90m│ \u001b[39m %6 = Base.not_int(%5)\u001b[36m::Bool\u001b[39m\n",
"\u001b[90m└──\u001b[39m goto #4 if not %6\n",
"\u001b[90m2 ┄\u001b[39m %8 = @_5::Tuple{Int64,Int64}\u001b[36m::Tuple{Int64,Int64}\u001b[39m\n",
"\u001b[90m│ \u001b[39m (i = Core.getfield(%8, 1))\n",
"\u001b[90m│ \u001b[39m %10 = Core.getfield(%8, 2)\u001b[36m::Int64\u001b[39m\n",
"\u001b[90m│ \u001b[39m %11 = S\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n",
"\u001b[90m│ \u001b[39m %12 = Base.getindex(f, i)\u001b[91m\u001b[1m::Function\u001b[22m\u001b[39m\n",
"\u001b[90m│ \u001b[39m %13 = (%12)(1.0)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n",
"\u001b[90m│ \u001b[39m (S = %11 + %13)\n",
"\u001b[90m│ \u001b[39m (@_5 = Base.iterate(%3, %10))\n",
"\u001b[90m│ \u001b[39m %16 = (@_5 === nothing)\u001b[36m::Bool\u001b[39m\n",
"\u001b[90m│ \u001b[39m %17 = Base.not_int(%16)\u001b[36m::Bool\u001b[39m\n",
"\u001b[90m└──\u001b[39m goto #4 if not %17\n",
"\u001b[90m3 ─\u001b[39m goto #2\n",
"\u001b[90m4 ┄\u001b[39m return S\n"
]
}
],
"source": [
"f0(t) = t^2\n",
"f1(t) = t^3\n",
"f = [f0,f1]\n",
"#################################\n",
"function F(f)\n",
" N = length(f)\n",
" S = 0.0\n",
" for i = 1:N\n",
" S += f[i](1.0)\n",
" end \n",
" return S \n",
"end\n",
"\n",
"#################################\n",
"@code_warntype F(f)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Variables\n",
" #self#\u001b[36m::Core.Compiler.Const(F, false)\u001b[39m\n",
" f\u001b[36m::Array{Function,1}\u001b[39m\n",
" N\u001b[36m::Int64\u001b[39m\n",
" S\u001b[36m::Float64\u001b[39m\n",
" @_5\u001b[33m\u001b[1m::Union{Nothing, Tuple{Int64,Int64}}\u001b[22m\u001b[39m\n",
" i\u001b[36m::Int64\u001b[39m\n",
"\n",
"Body\u001b[36m::Float64\u001b[39m\n",
"\u001b[90m1 ─\u001b[39m (N = Main.length(f))\n",
"\u001b[90m│ \u001b[39m (S = 0.0)\n",
"\u001b[90m│ \u001b[39m %3 = (1:N)\u001b[36m::Core.Compiler.PartialStruct(UnitRange{Int64}, Any[Core.Compiler.Const(1, false), Int64])\u001b[39m\n",
"\u001b[90m│ \u001b[39m (@_5 = Base.iterate(%3))\n",
"\u001b[90m│ \u001b[39m %5 = (@_5 === nothing)\u001b[36m::Bool\u001b[39m\n",
"\u001b[90m│ \u001b[39m %6 = Base.not_int(%5)\u001b[36m::Bool\u001b[39m\n",
"\u001b[90m└──\u001b[39m goto #4 if not %6\n",
"\u001b[90m2 ┄\u001b[39m %8 = @_5::Tuple{Int64,Int64}\u001b[36m::Tuple{Int64,Int64}\u001b[39m\n",
"\u001b[90m│ \u001b[39m (i = Core.getfield(%8, 1))\n",
"\u001b[90m│ \u001b[39m %10 = Core.getfield(%8, 2)\u001b[36m::Int64\u001b[39m\n",
"\u001b[90m│ \u001b[39m %11 = S\u001b[36m::Float64\u001b[39m\n",
"\u001b[90m│ \u001b[39m %12 = Base.getindex(f, i)\u001b[91m\u001b[1m::Function\u001b[22m\u001b[39m\n",
"\u001b[90m│ \u001b[39m %13 = (%12)(1.0)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n",
"\u001b[90m│ \u001b[39m %14 = Core.typeassert(%13, Main.Float64)\u001b[36m::Float64\u001b[39m\n",
"\u001b[90m│ \u001b[39m (S = %11 + %14)\n",
"\u001b[90m│ \u001b[39m (@_5 = Base.iterate(%3, %10))\n",
"\u001b[90m│ \u001b[39m %17 = (@_5 === nothing)\u001b[36m::Bool\u001b[39m\n",
"\u001b[90m│ \u001b[39m %18 = Base.not_int(%17)\u001b[36m::Bool\u001b[39m\n",
"\u001b[90m└──\u001b[39m goto #4 if not %18\n",
"\u001b[90m3 ─\u001b[39m goto #2\n",
"\u001b[90m4 ┄\u001b[39m return S\n"
]
}
],
"source": [
"f0(t::Float64)::Float64 = t^2\n",
"f1(t::Float64)::Float64 = t^3\n",
"f = [f0,f1]\n",
"#################################\n",
"function F(f)\n",
" N = length(f)\n",
" S = 0.0\n",
" for i = 1:N\n",
" S += f[i](1.0)::Float64\n",
" end \n",
" return S \n",
"end\n",
"\n",
"#################################\n",
"@code_warntype F(f)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Variables\n",
" #self#\u001b[36m::Core.Compiler.Const(F, false)\u001b[39m\n",
" f\u001b[36m::SArray{Tuple{2},Function,1,2}\u001b[39m\n",
" N\u001b[36m::Int64\u001b[39m\n",
" S\u001b[36m::Float64\u001b[39m\n",
" @_5\u001b[33m\u001b[1m::Union{Nothing, Tuple{Int64,Int64}}\u001b[22m\u001b[39m\n",
" i\u001b[36m::Int64\u001b[39m\n",
"\n",
"Body\u001b[36m::Float64\u001b[39m\n",
"\u001b[90m1 ─\u001b[39m (N = Main.length(f))\n",
"\u001b[90m│ \u001b[39m (S = 0.0)\n",
"\u001b[90m│ \u001b[39m %3 = (1:N::Core.Compiler.Const(2, false))\u001b[36m::Core.Compiler.Const(1:2, false)\u001b[39m\n",
"\u001b[90m│ \u001b[39m (@_5 = Base.iterate(%3))\n",
"\u001b[90m│ \u001b[39m %5 = (@_5::Core.Compiler.Const((1, 1), false) === nothing)\u001b[36m::Core.Compiler.Const(false, false)\u001b[39m\n",
"\u001b[90m│ \u001b[39m %6 = Base.not_int(%5)\u001b[36m::Core.Compiler.Const(true, false)\u001b[39m\n",
"\u001b[90m└──\u001b[39m goto #4 if not %6\n",
"\u001b[90m2 ┄\u001b[39m %8 = @_5::Tuple{Int64,Int64}\u001b[36m::Tuple{Int64,Int64}\u001b[39m\n",
"\u001b[90m│ \u001b[39m (i = Core.getfield(%8, 1))\n",
"\u001b[90m│ \u001b[39m %10 = Core.getfield(%8, 2)\u001b[36m::Int64\u001b[39m\n",
"\u001b[90m│ \u001b[39m %11 = S\u001b[36m::Float64\u001b[39m\n",
"\u001b[90m│ \u001b[39m %12 = Base.getindex(f, i)\u001b[91m\u001b[1m::Function\u001b[22m\u001b[39m\n",
"\u001b[90m│ \u001b[39m %13 = (%12)(1.0)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n",
"\u001b[90m│ \u001b[39m %14 = Core.typeassert(%13, Main.Float64)\u001b[36m::Float64\u001b[39m\n",
"\u001b[90m│ \u001b[39m (S = %11 + %14)\n",
"\u001b[90m│ \u001b[39m (@_5 = Base.iterate(%3, %10))\n",
"\u001b[90m│ \u001b[39m %17 = (@_5 === nothing)\u001b[36m::Bool\u001b[39m\n",
"\u001b[90m│ \u001b[39m %18 = Base.not_int(%17)\u001b[36m::Bool\u001b[39m\n",
"\u001b[90m└──\u001b[39m goto #4 if not %18\n",
"\u001b[90m3 ─\u001b[39m goto #2\n",
"\u001b[90m4 ┄\u001b[39m return S\n"
]
}
],
"source": [
"using StaticArrays\n",
"\n",
"g0(t)::Float64 = t^2\n",
"g1(t)::Float64 = t^3\n",
"const g = SVector(g0,g1)\n",
"#################################\n",
"function F(f)\n",
" N = length(f)\n",
" S = 0.0\n",
" for i = 1:N\n",
" S += f[i](1.0)::Float64\n",
" end \n",
" return S \n",
"end\n",
"\n",
"#################################\n",
"@code_warntype F(g)\n"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Variables\n",
" #self#\u001b[36m::Core.Compiler.Const(F, false)\u001b[39m\n",
" f\u001b[36m::Core.Compiler.Const((h0, h1), false)\u001b[39m\n",
" N\u001b[36m::Int64\u001b[39m\n",
" S\u001b[36m::Float64\u001b[39m\n",
" @_5\u001b[33m\u001b[1m::Union{Nothing, Tuple{Int64,Int64}}\u001b[22m\u001b[39m\n",
" i\u001b[36m::Int64\u001b[39m\n",
"\n",
"Body\u001b[36m::Float64\u001b[39m\n",
"\u001b[90m1 ─\u001b[39m (N = Main.length(f))\n",
"\u001b[90m│ \u001b[39m (S = 0.0)\n",
"\u001b[90m│ \u001b[39m %3 = (1:N::Core.Compiler.Const(2, false))\u001b[36m::Core.Compiler.Const(1:2, false)\u001b[39m\n",
"\u001b[90m│ \u001b[39m (@_5 = Base.iterate(%3))\n",
"\u001b[90m│ \u001b[39m %5 = (@_5::Core.Compiler.Const((1, 1), false) === nothing)\u001b[36m::Core.Compiler.Const(false, false)\u001b[39m\n",
"\u001b[90m│ \u001b[39m %6 = Base.not_int(%5)\u001b[36m::Core.Compiler.Const(true, false)\u001b[39m\n",
"\u001b[90m└──\u001b[39m goto #4 if not %6\n",
"\u001b[90m2 ┄\u001b[39m %8 = @_5::Tuple{Int64,Int64}\u001b[36m::Tuple{Int64,Int64}\u001b[39m\n",
"\u001b[90m│ \u001b[39m (i = Core.getfield(%8, 1))\n",
"\u001b[90m│ \u001b[39m %10 = Core.getfield(%8, 2)\u001b[36m::Int64\u001b[39m\n",
"\u001b[90m│ \u001b[39m %11 = S\u001b[36m::Float64\u001b[39m\n",
"\u001b[90m│ \u001b[39m %12 = Base.getindex(f, i)\u001b[91m\u001b[1m::Union{typeof(h0), typeof(h1)}\u001b[22m\u001b[39m\n",
"\u001b[90m│ \u001b[39m %13 = (%12)(1.0)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n",
"\u001b[90m│ \u001b[39m %14 = Core.typeassert(%13, Main.Float64)\u001b[36m::Float64\u001b[39m\n",
"\u001b[90m│ \u001b[39m (S = %11 + %14)\n",
"\u001b[90m│ \u001b[39m (@_5 = Base.iterate(%3, %10))\n",
"\u001b[90m│ \u001b[39m %17 = (@_5 === nothing)\u001b[36m::Bool\u001b[39m\n",
"\u001b[90m│ \u001b[39m %18 = Base.not_int(%17)\u001b[36m::Bool\u001b[39m\n",
"\u001b[90m└──\u001b[39m goto #4 if not %18\n",
"\u001b[90m3 ─\u001b[39m goto #2\n",
"\u001b[90m4 ┄\u001b[39m return S\n"
]
}
],
"source": [
"h0(t) = t^2\n",
"h1(t) = t^3\n",
"h = (h0,h1)\n",
"#################################\n",
"function F(f)\n",
" N = length(f)\n",
" S = 0.0\n",
" for i = 1:N\n",
" S += f[i](1.0)::Float64\n",
" end \n",
" return S \n",
"end\n",
"\n",
"#################################\n",
"@code_warntype F(h)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.3.1",
"language": "julia",
"name": "julia-1.3"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.3.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment