{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "14ea724a-be83-4452-ba62-31f2ee8cc2c9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "VERSION = v\"1.8.0-DEV.43\"\n", " 5.267 μs (0 allocations: 0 bytes)\n", " 1.617 ms (78980 allocations: 1.36 MiB)\n", " 38.900 μs (4 allocations: 156.41 KiB)\n", " 44.600 μs (4 allocations: 156.41 KiB)\n", " 46.100 μs (4 allocations: 156.41 KiB)\n", " 5.283 μs (1 allocation: 32 bytes)\n", " 1.597 ms (78980 allocations: 1.36 MiB)\n" ] } ], "source": [ "function g!(x, y)\n", " for i in 1:length(x)\n", " x[i] *= 3\n", " y[i] += x[i]\n", " end\n", " x, y\n", "end\n", "\n", "function h!()\n", " for i in 1:length(x_global)\n", " x_global[i] *= 3\n", " y_global[i] += x_global[i]\n", " end\n", "end\n", "\n", "function f0()\n", " x = randn(100, 100)\n", " y = randn(100, 100)\n", " x, y\n", "end\n", "\n", "function f1()\n", " x = randn(100, 100)\n", " y = randn(100, 100) \n", " g!(x, y)\n", "end\n", "\n", "function f2!(x = randn(100, 100), y = randn(100, 100))\n", " g!(x, y)\n", "end\n", "\n", "function f3!()\n", " g!(x_global, y_global)\n", "end\n", "\n", "function f4!()\n", " h!()\n", "end\n", "\n", "using BenchmarkTools\n", "\n", "x_global = randn(100, 100)\n", "y_global = randn(100, 100)\n", "\n", "@show VERSION\n", "@btime g!($x_global, $y_global)\n", "@btime h!()\n", "@btime f0()\n", "@btime f1()\n", "@btime f2!()\n", "@btime f3!()\n", "@btime f4!()" ] }, { "cell_type": "code", "execution_count": 2, "id": "e8127bc5-49dc-4919-a540-60dffdcaf70c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "MethodInstance for h!()\n", " from h!() in Main at In[1]:9\n", "Arguments\n", " #self#\u001b[36m::Core.Const(h!)\u001b[39m\n", "Locals\n", " @_2\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n", " i\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n", "Body\u001b[36m::Nothing\u001b[39m\n", "\u001b[90m1 ─\u001b[39m %1 = Main.length(Main.x_global)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n", "\u001b[90m│ \u001b[39m %2 = (1:%1)\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 = Base.getindex(Main.x_global, i)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n", "\u001b[90m│ \u001b[39m %11 = (%10 * 3)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n", "\u001b[90m│ \u001b[39m Base.setindex!(Main.x_global, %11, i)\n", "\u001b[90m│ \u001b[39m %13 = Base.getindex(Main.y_global, i)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n", "\u001b[90m│ \u001b[39m %14 = Base.getindex(Main.x_global, i)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n", "\u001b[90m│ \u001b[39m %15 = (%13 + %14)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n", "\u001b[90m│ \u001b[39m Base.setindex!(Main.y_global, %15, i)\n", "\u001b[90m│ \u001b[39m (@_2 = Base.iterate(%2, %9))\n", "\u001b[90m│ \u001b[39m %18 = (@_2 === nothing)\u001b[36m::Bool\u001b[39m\n", "\u001b[90m│ \u001b[39m %19 = Base.not_int(%18)\u001b[36m::Bool\u001b[39m\n", "\u001b[90m└──\u001b[39m goto #4 if not %19\n", "\u001b[90m3 ─\u001b[39m goto #2\n", "\u001b[90m4 ┄\u001b[39m return nothing\n", "\n" ] } ], "source": [ "@code_warntype h!()" ] }, { "cell_type": "code", "execution_count": 3, "id": "84462258-fdad-4068-959a-1c4336589dc2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "MethodInstance for g!(::\u001b[0mMatrix{Float64}, ::\u001b[0mMatrix{Float64})\n", " from g!(x, y) in Main at In[1]:1\n", "Arguments\n", " #self#\u001b[36m::Core.Const(g!)\u001b[39m\n", " x\u001b[36m::Matrix{Float64}\u001b[39m\n", " y\u001b[36m::Matrix{Float64}\u001b[39m\n", "Locals\n", " @_4\u001b[33m\u001b[1m::Union{Nothing, Tuple{Int64, Int64}}\u001b[22m\u001b[39m\n", " i\u001b[36m::Int64\u001b[39m\n", "Body\u001b[36m::Tuple{Matrix{Float64}, Matrix{Float64}}\u001b[39m\n", "\u001b[90m1 ─\u001b[39m %1 = Main.length(x)\u001b[36m::Int64\u001b[39m\n", "\u001b[90m│ \u001b[39m %2 = (1:%1)\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\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 = Base.getindex(x, i)\u001b[36m::Float64\u001b[39m\n", "\u001b[90m│ \u001b[39m %11 = (%10 * 3)\u001b[36m::Float64\u001b[39m\n", "\u001b[90m│ \u001b[39m Base.setindex!(x, %11, i)\n", "\u001b[90m│ \u001b[39m %13 = Base.getindex(y, i)\u001b[36m::Float64\u001b[39m\n", "\u001b[90m│ \u001b[39m %14 = Base.getindex(x, i)\u001b[36m::Float64\u001b[39m\n", "\u001b[90m│ \u001b[39m %15 = (%13 + %14)\u001b[36m::Float64\u001b[39m\n", "\u001b[90m│ \u001b[39m Base.setindex!(y, %15, i)\n", "\u001b[90m│ \u001b[39m (@_4 = Base.iterate(%2, %9))\n", "\u001b[90m│ \u001b[39m %18 = (@_4 === nothing)\u001b[36m::Bool\u001b[39m\n", "\u001b[90m│ \u001b[39m %19 = Base.not_int(%18)\u001b[36m::Bool\u001b[39m\n", "\u001b[90m└──\u001b[39m goto #4 if not %19\n", "\u001b[90m3 ─\u001b[39m goto #2\n", "\u001b[90m4 ┄\u001b[39m %22 = Core.tuple(x, y)\u001b[36m::Tuple{Matrix{Float64}, Matrix{Float64}}\u001b[39m\n", "\u001b[90m└──\u001b[39m return %22\n", "\n" ] } ], "source": [ "@code_warntype g!(x_global, y_global)" ] }, { "cell_type": "code", "execution_count": 4, "id": "4885a2b6-f608-49c7-b2fb-f1d248edd345", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "CodeInfo(\n", "\u001b[90m1 ─\u001b[39m %1 = Main.x_global\u001b[36m::Any\u001b[39m\n", "\u001b[90m│ \u001b[39m %2 = Main.y_global\u001b[36m::Any\u001b[39m\n", "\u001b[90m│ \u001b[39m %3 = Main.g!(%1, %2)\u001b[36m::Tuple{Any, Any}\u001b[39m\n", "\u001b[90m└──\u001b[39m return %3\n", ") => Tuple{Any, Any}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@code_typed f3!()" ] }, { "cell_type": "code", "execution_count": 5, "id": "2ffcd19e-bd18-491e-993f-66173d0c7595", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "CodeInfo(\n", "\u001b[90m1 ──\u001b[39m %1 = Base.arraylen(x)\u001b[36m::Int64\u001b[39m\n", "\u001b[90m│ \u001b[39m %2 = Base.sle_int(1, %1)\u001b[36m::Bool\u001b[39m\n", "\u001b[90m│ \u001b[39m %3 = Base.ifelse(%2, %1, 0)\u001b[36m::Int64\u001b[39m\n", "\u001b[90m│ \u001b[39m %4 = Base.slt_int(%3, 1)\u001b[36m::Bool\u001b[39m\n", "\u001b[90m└───\u001b[39m goto #3 if not %4\n", "\u001b[90m2 ──\u001b[39m Base.nothing\u001b[90m::Nothing\u001b[39m\n", "\u001b[90m└───\u001b[39m goto #4\n", "\u001b[90m3 ──\u001b[39m goto #4\n", "\u001b[90m4 ┄─\u001b[39m %9 = φ (#2 => true, #3 => false)\u001b[36m::Bool\u001b[39m\n", "\u001b[90m│ \u001b[39m %10 = φ (#3 => 1)\u001b[36m::Int64\u001b[39m\n", "\u001b[90m│ \u001b[39m %11 = φ (#3 => 1)\u001b[36m::Int64\u001b[39m\n", "\u001b[90m│ \u001b[39m %12 = Base.not_int(%9)\u001b[36m::Bool\u001b[39m\n", "\u001b[90m└───\u001b[39m goto #10 if not %12\n", "\u001b[90m5 ┄─\u001b[39m %14 = φ (#4 => %10, #9 => %29)\u001b[36m::Int64\u001b[39m\n", "\u001b[90m│ \u001b[39m %15 = φ (#4 => %11, #9 => %30)\u001b[36m::Int64\u001b[39m\n", "\u001b[90m│ \u001b[39m %16 = Base.arrayref(true, x, %14)\u001b[36m::Float64\u001b[39m\n", "\u001b[90m│ \u001b[39m %17 = Base.mul_float(%16, 3.0)\u001b[36m::Float64\u001b[39m\n", "\u001b[90m│ \u001b[39m Base.arrayset(true, x, %17, %14)\u001b[90m::Matrix{Float64}\u001b[39m\n", "\u001b[90m│ \u001b[39m %19 = Base.arrayref(true, y, %14)\u001b[36m::Float64\u001b[39m\n", "\u001b[90m│ \u001b[39m %20 = Base.arrayref(true, x, %14)\u001b[36m::Float64\u001b[39m\n", "\u001b[90m│ \u001b[39m %21 = Base.add_float(%19, %20)\u001b[36m::Float64\u001b[39m\n", "\u001b[90m│ \u001b[39m Base.arrayset(true, y, %21, %14)\u001b[90m::Matrix{Float64}\u001b[39m\n", "\u001b[90m│ \u001b[39m %23 = (%15 === %3)\u001b[36m::Bool\u001b[39m\n", "\u001b[90m└───\u001b[39m goto #7 if not %23\n", "\u001b[90m6 ──\u001b[39m Base.nothing\u001b[90m::Nothing\u001b[39m\n", "\u001b[90m└───\u001b[39m goto #8\n", "\u001b[90m7 ──\u001b[39m %27 = Base.add_int(%15, 1)\u001b[36m::Int64\u001b[39m\n", "\u001b[90m└───\u001b[39m goto #8\n", "\u001b[90m8 ┄─\u001b[39m %29 = φ (#7 => %27)\u001b[36m::Int64\u001b[39m\n", "\u001b[90m│ \u001b[39m %30 = φ (#7 => %27)\u001b[36m::Int64\u001b[39m\n", "\u001b[90m│ \u001b[39m %31 = φ (#6 => true, #7 => false)\u001b[36m::Bool\u001b[39m\n", "\u001b[90m│ \u001b[39m %32 = Base.not_int(%31)\u001b[36m::Bool\u001b[39m\n", "\u001b[90m└───\u001b[39m goto #10 if not %32\n", "\u001b[90m9 ──\u001b[39m goto #5\n", "\u001b[90m10 ┄\u001b[39m %35 = Core.tuple(x, y)\u001b[36m::Tuple{Matrix{Float64}, Matrix{Float64}}\u001b[39m\n", "\u001b[90m└───\u001b[39m return %35\n", ") => Tuple{Matrix{Float64}, Matrix{Float64}}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@code_typed g!(x_global, y_global)" ] }, { "cell_type": "code", "execution_count": 6, "id": "a6aec35a-ffb5-464f-a0df-bd7c7140809c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "MethodInstance for f3!()\n", " from f3!() in Main at In[1]:32\n", "Arguments\n", " #self#\u001b[36m::Core.Const(f3!)\u001b[39m\n", "Body\u001b[91m\u001b[1m::Tuple{Any, Any}\u001b[22m\u001b[39m\n", "\u001b[90m1 ─\u001b[39m %1 = Main.g!(Main.x_global, Main.y_global)\u001b[91m\u001b[1m::Tuple{Any, Any}\u001b[22m\u001b[39m\n", "\u001b[90m└──\u001b[39m return %1\n", "\n" ] } ], "source": [ "@code_warntype f3!()" ] }, { "cell_type": "code", "execution_count": null, "id": "b2fcf0dd-69f9-4623-b0f6-a8c21aafe32d", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "formats": "ipynb,jl:hydrogen" }, "kernelspec": { "display_name": "Julia 1.8.0-DEV", "language": "julia", "name": "julia-1.8" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.8.0" } }, "nbformat": 4, "nbformat_minor": 5 }