Last active
January 4, 2021 00:39
-
-
Save genkuroki/7cd004d6de5ffb15fb3c53362ce0e8cc to your computer and use it in GitHub Desktop.
Anyの配列による速度劣化
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": {}, | |
"cell_type": "markdown", | |
"source": "# Anyの配列による速度劣化" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "using Statistics, BenchmarkTools\n\nfunction rnorm(N)\n A = [] # <------- 問題有り!\n for i in 1:N\n push!(A, randn())\n end\n A # <------- Anyの配列が返される\nend\n\nA_Any = rnorm(10^7)\n@show typeof(A_Any), length(A_Any)\n\nA_Float64 = Vector{typeof(A_Any[begin])}(A_Any)\n@show typeof(A_Float64), length(A_Float64)\n\nmeansquare(A) = mean(x -> x^2, A)\n\nprint(\"Vector{Any}: \")\n@btime meansquare($A_Any)\nprint(\"Vector{Float64}:\")\n@btime meansquare($A_Float64)", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "(typeof(A_Any), length(A_Any)) = (Vector{Any}, 10000000)\n(typeof(A_Float64), length(A_Float64)) = (Vector{Float64}, 10000000)\nVector{Any}: 789.277 ms (30000004 allocations: 457.76 MiB)\nVector{Float64}: 4.339 ms (0 allocations: 0 bytes)\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 1, | |
"data": { | |
"text/plain": "0.9999444123170562" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "julia-1.7-depwarn-o3", | |
"display_name": "Julia 1.7.0-DEV depwarn -O3", | |
"language": "julia" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": true, | |
"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.7.0" | |
}, | |
"@webio": { | |
"lastKernelId": null, | |
"lastCommId": null | |
}, | |
"gist": { | |
"id": "7cd004d6de5ffb15fb3c53362ce0e8cc", | |
"data": { | |
"description": "Anyの配列による速度劣化", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/7cd004d6de5ffb15fb3c53362ce0e8cc" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment