Created
July 7, 2020 06:54
-
-
Save genkuroki/1e171e409f495051e0f93edd0ce6c27c to your computer and use it in GitHub Desktop.
memory allocation - array of arrays vs 2d array - Julia v1.5.0-rc1
This file contains 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": { | |
"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": "using Random: seed!\nseed!(12345)\n\nm = 3\nn = 4\nv = [rand(0.01:0.01:0.99, m) for i in 1:n]\nV = hcat(v...);", | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function f!(w, v)\n @inbounds for i in 1:length(v)\n @. w[i] = v[i] + i\n end\nend\n\nw = [zeros(m) for i in 1:n]\n\n@show v\n@show w\nprintln()\n@time f!(w, v)\n@time f!(w, v)\nprintln()\n@show w;", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "v = [[0.59, 0.7, 0.03], [0.35, 0.4, 0.87], [0.9, 0.69, 0.9], [0.34, 0.94, 0.5]]\nw = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]\n\n 0.059803 seconds (176.85 k allocations: 9.172 MiB)\n 0.000007 seconds\n\nw = [[1.5899999999999999, 1.7, 1.03], [2.35, 2.4, 2.87], [3.9, 3.69, 3.9], [4.34, 4.9399999999999995, 4.5]]\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"scrolled": false, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function g!(w, v)\n @inbounds for i in 1:size(v,2)\n @. @views w[:,i] = v[:,i] + i\n end\nend\n\nW = zeros(3, n)\n\n@show V'\n@show W'\nprintln()\n@time g!(W, V)\n@time g!(W, V)\nprintln()\n@show W';", | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "V' = [0.59 0.7 0.03; 0.35 0.4 0.87; 0.9 0.69 0.9; 0.34 0.94 0.5]\nW' = [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]\n\n 0.145931 seconds (406.10 k allocations: 20.830 MiB)\n 0.000002 seconds\n\nW' = [1.5899999999999999 1.7 1.03; 2.35 2.4 2.87; 3.9 3.69 3.9; 4.34 4.9399999999999995 4.5]\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "using UnsafeArrays\n\nfunction F!(w, v)\n @inbounds @uviews w v for i in 1:length(v)\n @. w[i] = v[i] + i\n end\nend\n\nz = [zeros(m) for i in 1:n]\n\n@show v\n@show z\nprintln()\n@time F!(z, v)\n@time F!(z, v)\nprintln()\n@show z;", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "v = [[0.59, 0.7, 0.03], [0.35, 0.4, 0.87], [0.9, 0.69, 0.9], [0.34, 0.94, 0.5]]\nz = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]\n\n 0.023941 seconds (27.68 k allocations: 1.438 MiB)\n 0.000003 seconds\n\nz = [[1.5899999999999999, 1.7, 1.03], [2.35, 2.4, 2.87], [3.9, 3.69, 3.9], [4.34, 4.9399999999999995, 4.5]]\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"scrolled": false, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "using UnsafeArrays\n\nfunction G!(w, v)\n @inbounds @uviews w v for i in 1:size(v,2)\n @. @views w[:,i] = v[:,i] + i\n end\nend\n\nZ = zeros(m, n)\n\n@show V'\n@show Z'\nprintln()\n@time G!(Z, V)\n@time G!(Z, V)\nprintln()\n@show Z';", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "V' = [0.59 0.7 0.03; 0.35 0.4 0.87; 0.9 0.69 0.9; 0.34 0.94 0.5]\nZ' = [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]\n\n 0.139889 seconds (487.38 k allocations: 23.816 MiB, 6.31% gc time)\n 0.000006 seconds\n\nZ' = [1.5899999999999999 1.7 1.03; 2.35 2.4 2.87; 3.9 3.69 3.9; 4.34 4.9399999999999995 4.5]\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "using BenchmarkTools", | |
"execution_count": 7, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "m = 3\nn = 10^6\n\nv = [rand(m) for i in 1:n]\nV = hcat(v...)\n\nw = [zeros(m) for i in 1:n]\nz = [zeros(m) for i in 1:n]\nW = zeros(m, n)\nZ = zeros(m, n)\n\nf!(w, v)\ng!(W, V)\nF!(z, v)\nG!(Z, V)\n\nhcat(w...) == hcat(z...) == W == Z", | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 8, | |
"data": { | |
"text/plain": "true" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@benchmark f!(w, v)", | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 9, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 0 bytes\n allocs estimate: 0\n --------------\n minimum time: 20.183 ms (0.00% GC)\n median time: 21.248 ms (0.00% GC)\n mean time: 21.357 ms (0.00% GC)\n maximum time: 26.757 ms (0.00% GC)\n --------------\n samples: 235\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@benchmark g!(W, V)", | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 10, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 0 bytes\n allocs estimate: 0\n --------------\n minimum time: 9.058 ms (0.00% GC)\n median time: 9.282 ms (0.00% GC)\n mean time: 9.469 ms (0.00% GC)\n maximum time: 18.085 ms (0.00% GC)\n --------------\n samples: 528\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@benchmark F!(z, v)", | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 11, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 0 bytes\n allocs estimate: 0\n --------------\n minimum time: 20.331 ms (0.00% GC)\n median time: 21.369 ms (0.00% GC)\n mean time: 21.592 ms (0.00% GC)\n maximum time: 27.942 ms (0.00% GC)\n --------------\n samples: 232\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@benchmark G!(Z, V)", | |
"execution_count": 12, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 12, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 0 bytes\n allocs estimate: 0\n --------------\n minimum time: 6.427 ms (0.00% GC)\n median time: 6.742 ms (0.00% GC)\n mean time: 6.827 ms (0.00% GC)\n maximum time: 14.927 ms (0.00% GC)\n --------------\n samples: 733\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"@webio": { | |
"lastKernelId": null, | |
"lastCommId": null | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/1e171e409f495051e0f93edd0ce6c27c" | |
}, | |
"gist": { | |
"id": "1e171e409f495051e0f93edd0ce6c27c", | |
"data": { | |
"description": "memory allocation - array of arrays vs 2d array - Julia v1.5.0-rc1", | |
"public": true | |
} | |
}, | |
"kernelspec": { | |
"name": "julia-1.5", | |
"display_name": "Julia 1.5.0-rc1", | |
"language": "julia" | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"name": "julia", | |
"mimetype": "application/julia", | |
"version": "1.5.0" | |
}, | |
"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 | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment