Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Created July 14, 2020 12:32
Show Gist options
  • Save genkuroki/b03a289bb6f2d3accf63f5106e87e343 to your computer and use it in GitHub Desktop.
Save genkuroki/b03a289bb6f2d3accf63f5106e87e343 to your computer and use it in GitHub Desktop.
Avoid fields with abstract type
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "**References**\n\n* https://docs.julialang.org/en/v1/manual/performance-tips/index.html#Avoid-fields-with-abstract-type-1\n* https://github.com/abap34/DeepShiba.jl"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "VERSION",
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 1,
"data": {
"text/plain": "v\"1.5.0-rc1.0\""
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "]status DeepShiba",
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": "\u001b[32m\u001b[1mStatus\u001b[22m\u001b[39m `C:\\Users\\genkuroki\\.julia\\environments\\v1.5\\Project.toml`\n \u001b[90m [bc2267fa] \u001b[39m\u001b[37mDeepShiba v0.1.0 `https://github.com/abap34/DeepShiba.jl#master`\u001b[39m\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using BenchmarkTools",
"execution_count": 3,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## スカラーの和"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = 1.2\nb = 3.4\n@btime $a + $b",
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": " 0.001 ns (0 allocations: 0 bytes)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "4.6"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "struct Foo{T}\n data::T\nend\nBase.:+(x::Foo, y::Foo) = x.data + y.data\n\na_foo = Foo(1.2)\nb_foo = Foo(3.4)\n@btime $a_foo + $b_foo",
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": " 0.001 ns (0 allocations: 0 bytes)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "4.6"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "const NullableRealValueType = Union{Real, AbstractArray{<:Real}, Nothing}\nstruct Bar\n data::NullableRealValueType\nend\nBase.:+(x::Bar, y::Bar) = x.data + y.data\n\na_bar = Bar(1.2)\nb_bar = Bar(3.4)\n@btime $a_bar + $b_bar",
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": " 0.001 ns (0 allocations: 0 bytes)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "4.6"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using DeepShiba\n\na_var = variable(1.2)\nb_var = variable(3.4)\n@btime $a_var + $b_var",
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": " 1.970 μs (19 allocations: 784 bytes)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": ""
},
"metadata": {}
},
{
"output_type": "stream",
"text": "{DeepShiba.ShibaObject.Variable}:\ndata: 4.6\ncreator: DeepShiba.Add\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## 縦ベクトルと横ベクトルの和"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = [100, 200]\nb = [5, 7, 10]'\n@btime $a .+ $b",
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": " 51.784 ns (1 allocation: 128 bytes)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "2×3 Array{Int64,2}:\n 105 107 110\n 205 207 210"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "struct Foo{T}\n data::T\nend\nBase.:+(x::Foo, y::Foo) = x.data + y.data\nBase.broadcasted(::typeof(+), x::Foo, y::Foo) = x.data .+ y.data\n\na_foo = Foo([100, 200])\nb_foo = Foo([5, 7, 10]')\n@btime $a_foo .+ $b_foo",
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": " 52.717 ns (1 allocation: 128 bytes)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": "2×3 Array{Int64,2}:\n 105 107 110\n 205 207 210"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "const NullableRealValueType = Union{Real, AbstractArray{<:Real}, Nothing}\nstruct Bar\n data::NullableRealValueType\nend\nBase.:+(x::Bar, y::Bar) = x.data + y.data\nBase.broadcasted(::typeof(+), x::Bar, y::Bar) = x.data .+ y.data\n\na_bar = Bar([100, 200])\nb_bar = Bar([5, 7, 10]')\n@btime $a_bar .+ $b_bar",
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": " 394.589 ns (3 allocations: 192 bytes)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "2×3 Array{Int64,2}:\n 105 107 110\n 205 207 210"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using DeepShiba\nusing DeepShiba: Func, Add, _Add\n\nBase.Broadcast.broadcastable(V::Variable) = Ref(V)\nforward_dot(f::Add, x1, x2) = x1 .+ x2\nfunction Base.broadcasted(f::Func, vars::Variable...)\n f.inputs = [vars...]\n xs = [x.data for x in vars]\n ys = [forward_dot(f, xs...)]\n f.generation = minimum([x.generation for x in f.inputs])\n outputs = [Variable(y, f, nothing, f.generation - 1, \"\") for y in ys] \n f.outputs = outputs\n return length(outputs) == 1 ? outputs[1] : outputs\nend\nfunction Base.broadcasted(::typeof(_Add), x1::Variable, x2::Variable)\n DeepShiba.Add(nothing, nothing, 0).(x1, x2)\nend\nBase.broadcasted(::typeof(+), x1::Variable, x2::Variable) = _Add.(x1, x2)\n\na_var = variable([100, 200])\nb_var = variable([5, 7, 10]')\n@btime $a_var .+ $b_var",
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": " 1.970 μs (19 allocations: 1.03 KiB)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 11,
"data": {
"text/plain": ""
},
"metadata": {}
},
{
"output_type": "stream",
"text": "{DeepShiba.ShibaObject.Variable}:\ndata: [105 107 110; 205 207 210]\ncreator: Add\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function Base.broadcasted(f::Func, vars::Variable...)\n f.inputs = collect(vars)\n xs = (x -> x.data).(vars)\n ys = forward_dot(f, xs...)\n f.generation = minimum(x.generation for x in f.inputs)\n outputs = Variable(ys, f, nothing, f.generation - 1, \"\") \n f.outputs = outputs\n return outputs\nend\n\na_var = variable([100, 200])\nb_var = variable([5, 7, 10]')\n@btime $a_var .+ $b_var",
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"text": " 402.805 ns (5 allocations: 320 bytes)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 12,
"data": {
"text/plain": ""
},
"metadata": {}
},
{
"output_type": "stream",
"text": "{DeepShiba.ShibaObject.Variable}:\ndata: [105 107 110; 205 207 210]\ncreator: Add\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.5",
"display_name": "Julia 1.5.0-rc1",
"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.5.0"
},
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"gist": {
"id": "b03a289bb6f2d3accf63f5106e87e343",
"data": {
"description": "Avoid fields with abstract type",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/b03a289bb6f2d3accf63f5106e87e343"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment