Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genkuroki/f1076b9c5b00cd45a29f456cf3cb3f7a to your computer and use it in GitHub Desktop.
Save genkuroki/f1076b9c5b00cd45a29f456cf3cb3f7a to your computer and use it in GitHub Desktop.
memory allocation - array of arrays vs 2d array - Julia v1.4.2
{
"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.4.2\""
},
"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.13, 0.07, 0.81], [0.75, 0.55, 0.68], [0.17, 0.17, 0.02], [0.05, 0.18, 0.63]]\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.078492 seconds (183.05 k allocations: 9.366 MiB, 13.76% gc time)\n 0.000004 seconds\n\nw = [[1.13, 1.07, 1.81], [2.75, 2.55, 2.68], [3.17, 3.17, 3.02], [4.05, 4.18, 4.63]]\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.13 0.07 0.81; 0.75 0.55 0.68; 0.17 0.17 0.02; 0.05 0.18 0.63]\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.135985 seconds (431.13 k allocations: 21.799 MiB)\n 0.000002 seconds (8 allocations: 384 bytes)\n\nW' = [1.13 1.07 1.81; 2.75 2.55 2.68; 3.17 3.17 3.02; 4.05 4.18 4.63]\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.13, 0.07, 0.81], [0.75, 0.55, 0.68], [0.17, 0.17, 0.02], [0.05, 0.18, 0.63]]\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.050896 seconds (30.59 k allocations: 1.516 MiB, 54.56% gc time)\n 0.000004 seconds\n\nz = [[1.13, 1.07, 1.81], [2.75, 2.55, 2.68], [3.17, 3.17, 3.02], [4.05, 4.18, 4.63]]\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.13 0.07 0.81; 0.75 0.55 0.68; 0.17 0.17 0.02; 0.05 0.18 0.63]\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.158877 seconds (536.10 k allocations: 25.958 MiB)\n 0.000002 seconds\n\nZ' = [1.13 1.07 1.81; 2.75 2.55 2.68; 3.17 3.17 3.02; 4.05 4.18 4.63]\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: 19.761 ms (0.00% GC)\n median time: 20.927 ms (0.00% GC)\n mean time: 21.071 ms (0.00% GC)\n maximum time: 29.727 ms (0.00% GC)\n --------------\n samples: 238\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: 91.55 MiB\n allocs estimate: 2000000\n --------------\n minimum time: 34.016 ms (0.00% GC)\n median time: 51.013 ms (0.00% GC)\n mean time: 61.128 ms (29.18% GC)\n maximum time: 92.096 ms (46.05% GC)\n --------------\n samples: 82\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.202 ms (0.00% GC)\n median time: 21.308 ms (0.00% GC)\n mean time: 21.462 ms (0.00% GC)\n maximum time: 26.710 ms (0.00% GC)\n --------------\n samples: 233\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.248 ms (0.00% GC)\n median time: 6.561 ms (0.00% GC)\n mean time: 6.926 ms (0.00% GC)\n maximum time: 16.262 ms (0.00% GC)\n --------------\n samples: 721\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/f1076b9c5b00cd45a29f456cf3cb3f7a"
},
"gist": {
"id": "f1076b9c5b00cd45a29f456cf3cb3f7a",
"data": {
"description": "memory allocation - array of arrays vs 2d array - Julia v1.4.2",
"public": true
}
},
"kernelspec": {
"name": "julia-1.4",
"display_name": "Julia 1.4.2",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.4.2"
},
"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