Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active July 12, 2020 01:56
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/decd12123aa823357862edc7ab334e5e to your computer and use it in GitHub Desktop.
Save genkuroki/decd12123aa823357862edc7ab334e5e to your computer and use it in GitHub Desktop.
in-place calculation
Display the source blob
Display the rendered blob
Raw
{
"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 BenchmarkTools",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function update(u, h)\n u + h*u\nend\n\nfunction iter_update(u, h, N)\n for i in 1:N\n u = update(u, h)\n end\n u\nend",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "iter_update (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "u = ones(1024^2)\nN = 1024\nh = 1/N\nu_new = iter_update(u, h, N)\nu_new[1:3]",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "3-element Array{Float64,1}:\n 2.7169557294664357\n 2.7169557294664357\n 2.7169557294664357"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@btime u_new = iter_update(u, h, N);",
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": " 8.134 s (4096 allocations: 16.00 GiB)\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function update!(u, h)\n @. u += h*u\nend\n\nfunction iter_update!(u, h, N)\n for i in 1:N\n update!(u, h)\n end\nend",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "iter_update! (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "u = ones(1024^2)\nN = 1024\nh = 1/N\niter_update!(u, h, N)\nu[1:3]",
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "3-element Array{Float64,1}:\n 2.7169557294664357\n 2.7169557294664357\n 2.7169557294664357"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@btime iter_update!(u, h, N);",
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": " 468.062 ms (0 allocations: 0 bytes)\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"_draft": {
"nbviewer_url": "https://gist.github.com/decd12123aa823357862edc7ab334e5e"
},
"gist": {
"id": "decd12123aa823357862edc7ab334e5e",
"data": {
"description": "in-place calculation",
"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