Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Created January 23, 2018 05:02
Show Gist options
  • Save genkuroki/5962b82f551fa2ed10ca22e5523864ba to your computer and use it in GitHub Desktop.
Save genkuroki/5962b82f551fa2ed10ca22e5523864ba to your computer and use it in GitHub Desktop.
@view の使い方
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# @view の使い方\n\n黒木玄\n\n2018-01-23\n\n配列の部分配列をコピーせずに参照渡しにするには `@view` マクロを使う."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "N = 10^8\nn = 3\n\n# A.^2 を B にコピーする函数のつもり\n#\nfunction test!(B, A)\n B .= A.^2\nend",
"execution_count": 1,
"outputs": [
{
"data": {
"text/plain": "test! (generic function with 1 method)"
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "A = reshape(collect(1:2N), N, 2)\nA[1:n,:]",
"execution_count": 2,
"outputs": [
{
"data": {
"text/plain": "3×2 Array{Int64,2}:\n 1 100000001\n 2 100000002\n 3 100000003"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# A[:,2] に A[:,1].^2 がコピーされない上に遅い.\n\nA = reshape(collect(1:2N), N, 2)\ntest!(A[:,2], A[:,1])\n\nA = reshape(collect(1:2N), N, 2)\n@time test!(A[:,2], A[:,1])\nA[1:n,:]",
"execution_count": 3,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": " 0.984741 seconds (89 allocations: 1.490 GiB, 23.17% gc time)\n"
},
{
"data": {
"text/plain": "3×2 Array{Int64,2}:\n 1 100000001\n 2 100000002\n 3 100000003"
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# @view(A[:,2]) を函数に渡すと A[:,1].^2 がコピーされる.\n\nA = reshape(collect(1:2N), N, 2)\ntest!(@view(A[:,2]), A[:,1])\n\nA = reshape(collect(1:2N), N, 2)\n@time test!(@view(A[:,2]), A[:,1])\nA[1:n,:]",
"execution_count": 4,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": " 0.572699 seconds (33 allocations: 762.940 MiB, 20.10% gc time)\n"
},
{
"data": {
"text/plain": "3×2 Array{Int64,2}:\n 1 1\n 2 4\n 3 9"
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@macroexpand test!(@view(A[:,2]), A[:,1])",
"execution_count": 5,
"outputs": [
{
"data": {
"text/plain": ":(test!(true && (view)(A, :, 2), A[:, 1]))"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# さらに @view(A[:,1]) とすると速くなり, メモリ効率が大幅に改善される.\n\nA = reshape(collect(1:2N), N, 2)\ntest!(@view(A[:,2]), @view(A[:,1]))\n\nA = reshape(collect(1:2N), N, 2)\n@time test!(@view(A[:,2]), @view(A[:,1]))\nA[1:n,:]",
"execution_count": 6,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": " 0.146387 seconds (56 allocations: 1.188 KiB)\n"
},
{
"data": {
"text/plain": "3×2 Array{Int64,2}:\n 1 1\n 2 4\n 3 9"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@macroexpand test!(@view(A[:,2]), @view(A[:,1]))",
"execution_count": 7,
"outputs": [
{
"data": {
"text/plain": ":(test!(true && (view)(A, :, 2), true && (view)(A, :, 1)))"
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@which view(A, :, 2)",
"execution_count": 8,
"outputs": [
{
"data": {
"text/html": "view(A::<b>AbstractArray</b>, I...) at <a href=\"https://github.com/JuliaLang/julia/tree/d386e40c17d43b79fc89d3e579fc04547241787c/base/subarray.jl#L111\" target=\"_blank\">subarray.jl:111</a>",
"text/plain": "view(A::AbstractArray, I...) in Base at subarray.jl:111"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# マクロではない view を使っても同じことをできる.\n\nA = reshape(collect(1:2N), N, 2)\ntest!(view(A,:,2), view(A,:,1))\n\nA = reshape(collect(1:2N), N, 2)\n@time test!(view(A,:,2), view(A,:,1))\nA[1:n,:]",
"execution_count": 9,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": " 0.146301 seconds (56 allocations: 1.188 KiB)\n"
},
{
"data": {
"text/plain": "3×2 Array{Int64,2}:\n 1 1\n 2 4\n 3 9"
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# @views を使えばさらにシンプルに書ける.\n\nA = reshape(collect(1:2N), N, 2)\n@views test!(A[:,2], A[:,1])\n\nA = reshape(collect(1:2N), N, 2)\n@time @views test!(A[:,2], A[:,1])\nA[1:n,:]",
"execution_count": 10,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": " 0.142722 seconds (60 allocations: 1.250 KiB)\n"
},
{
"data": {
"text/plain": "3×2 Array{Int64,2}:\n 1 1\n 2 4\n 3 9"
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@macroexpand @views test!(A[:,2], A[:,1])",
"execution_count": 11,
"outputs": [
{
"data": {
"text/plain": ":(test!((Base.maybeview)(A, :, 2), (Base.maybeview)(A, :, 1)))"
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@which Base.maybeview(A, :, 2)",
"execution_count": 12,
"outputs": [
{
"data": {
"text/html": "maybeview(A::<b>AbstractArray</b>, args...) at <a href=\"https://github.com/JuliaLang/julia/tree/d386e40c17d43b79fc89d3e579fc04547241787c/base/subarray.jl#L469\" target=\"_blank\">subarray.jl:469</a>",
"text/plain": "maybeview(A::AbstractArray, args...) in Base at subarray.jl:469"
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-0.6",
"display_name": "Julia 0.6.2",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "0.6.2"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"gist": {
"id": "",
"data": {
"description": "@view の使い方",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment