Created
July 5, 2020 17:58
-
-
Save genkuroki/833e28971835b9d77911cd6f7aa4aa3e to your computer and use it in GitHub Desktop.
Arrays of Any and push!
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": "using BenchmarkTools\n\nn = 10^6\nA_Float64 = randn(n)\nA_Any = Array{Any, 1}(A_Float64)\nA_Real = Array{Real, 1}(A_Float64)\n\n@show typeof(A_Float64)\n@show typeof(A_Any)\n@show typeof(A_Real)\n\nGC.gc()", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "typeof(A_Float64) = Array{Float64,1}\ntypeof(A_Any) = Array{Any,1}\ntypeof(A_Real) = Array{Real,1}\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "sin.(A_Float64)\n@time sin.(A_Float64)\n\nsin.(A_Any)\n@time sin.(A_Any)\n\nsin.(A_Real)\n@time sin.(A_Real);", | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": " 0.122746 seconds (4 allocations: 7.630 MiB, 88.64% gc time)\n 0.080573 seconds (2.00 M allocations: 38.139 MiB, 5.99% gc time)\n 0.079812 seconds (2.00 M allocations: 38.139 MiB, 4.25% gc time)\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@benchmark sin.($A_Float64)", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 3, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 7.63 MiB\n allocs estimate: 2\n --------------\n minimum time: 13.572 ms (0.00% GC)\n median time: 14.287 ms (0.00% GC)\n mean time: 15.327 ms (4.26% GC)\n maximum time: 24.621 ms (21.33% GC)\n --------------\n samples: 326\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@benchmark sin.($A_Any)", | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 4, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 38.14 MiB\n allocs estimate: 1999495\n --------------\n minimum time: 73.765 ms (0.00% GC)\n median time: 80.746 ms (2.21% GC)\n mean time: 83.134 ms (1.97% GC)\n maximum time: 122.385 ms (1.88% GC)\n --------------\n samples: 61\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@benchmark sin.($A_Real)", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 5, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 38.14 MiB\n allocs estimate: 1999495\n --------------\n minimum time: 72.120 ms (0.00% GC)\n median time: 75.822 ms (2.40% GC)\n mean time: 81.108 ms (2.10% GC)\n maximum time: 117.274 ms (2.20% GC)\n --------------\n samples: 62\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "sum(A_Float64)\n@time sum(A_Float64)\n\nsum(A_Any)\n@time sum(A_Any)\n\nsum(A_Real)\n@time sum(A_Real);", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": " 0.000456 seconds (1 allocation: 16 bytes)\n 0.031495 seconds (1000.00 k allocations: 15.259 MiB, 11.02% gc time)\n 0.028434 seconds (1000.00 k allocations: 15.259 MiB)\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@benchmark sum($A_Float64)", | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 7, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 0 bytes\n allocs estimate: 0\n --------------\n minimum time: 277.120 μs (0.00% GC)\n median time: 304.179 μs (0.00% GC)\n mean time: 311.184 μs (0.00% GC)\n maximum time: 9.561 ms (0.00% GC)\n --------------\n samples: 10000\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@benchmark sum($A_Any)", | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 8, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 15.26 MiB\n allocs estimate: 999999\n --------------\n minimum time: 28.452 ms (0.00% GC)\n median time: 30.242 ms (0.00% GC)\n mean time: 31.833 ms (1.29% GC)\n maximum time: 52.589 ms (3.03% GC)\n --------------\n samples: 157\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@benchmark sum($A_Real)", | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 9, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 15.26 MiB\n allocs estimate: 999999\n --------------\n minimum time: 28.514 ms (0.00% GC)\n median time: 30.238 ms (0.00% GC)\n mean time: 31.663 ms (1.30% GC)\n maximum time: 55.418 ms (0.00% GC)\n --------------\n samples: 158\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function f_setindex!(L)\n a = Array{Float64, 1}(undef, L)\n for k in 1:L\n a[k] = sin(k)\n end\n a\nend\n\nL = 10^6\n@benchmark a = f_setindex!(L)", | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 10, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 7.63 MiB\n allocs estimate: 2\n --------------\n minimum time: 18.229 ms (0.00% GC)\n median time: 20.816 ms (0.00% GC)\n mean time: 21.822 ms (3.33% GC)\n maximum time: 57.157 ms (0.00% GC)\n --------------\n samples: 229\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function f_Float64_push!(L)\n a = Float64[]\n for k in 1:L\n push!(a, sin(k))\n end\n a\nend\n\nL = 10^6\n@benchmark a = f_Float64_push!(L)", | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 11, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 9.00 MiB\n allocs estimate: 20\n --------------\n minimum time: 25.098 ms (0.00% GC)\n median time: 26.157 ms (0.00% GC)\n mean time: 27.040 ms (0.00% GC)\n maximum time: 46.160 ms (0.00% GC)\n --------------\n samples: 180\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function f_Any_push!(L)\n a = []\n for k in 1:L\n push!(a, sin(k))\n end\n a\nend\n\nL = 10^6\n@benchmark a = f_Any_push!(L)", | |
"execution_count": 12, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 12, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 24.26 MiB\n allocs estimate: 1000020\n --------------\n minimum time: 30.733 ms (0.00% GC)\n median time: 37.638 ms (0.00% GC)\n mean time: 37.342 ms (10.59% GC)\n maximum time: 70.505 ms (53.34% GC)\n --------------\n samples: 134\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "julia-1.4", | |
"display_name": "Julia 1.4.2", | |
"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.4.2" | |
}, | |
"@webio": { | |
"lastKernelId": null, | |
"lastCommId": null | |
}, | |
"gist": { | |
"id": "833e28971835b9d77911cd6f7aa4aa3e", | |
"data": { | |
"description": "Arrays of Any and push!", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/833e28971835b9d77911cd6f7aa4aa3e" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment