Skip to content

Instantly share code, notes, and snippets.

@shinaoka
Created July 17, 2021 10:04
Show Gist options
  • Select an option

  • Save shinaoka/2a48ebff1b7b4e7bf126d379e9653098 to your computer and use it in GitHub Desktop.

Select an option

Save shinaoka/2a48ebff1b7b4e7bf126d379e9653098 to your computer and use it in GitHub Desktop.
Julia broadcasting demo
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "alone-valve",
"metadata": {},
"outputs": [],
"source": [
"using BenchmarkTools"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "dedicated-vegetable",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1000"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"N = 1000"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "usual-delivery",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 23.81 KiB\n",
" allocs estimate: 3\n",
" --------------\n",
" minimum time: 1.842 μs (0.00% GC)\n",
" median time: 2.640 μs (0.00% GC)\n",
" mean time: 3.430 μs (16.80% GC)\n",
" maximum time: 161.696 μs (96.76% GC)\n",
" --------------\n",
" samples: 10000\n",
" evals/sample: 9"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function test1(a,b)\n",
" a[:] += 2 * b\n",
"end\n",
"\n",
"a = zeros((N,))\n",
"b = ones((N,))\n",
"@benchmark test1(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "close-deficit",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 0 bytes\n",
" allocs estimate: 0\n",
" --------------\n",
" minimum time: 129.597 ns (0.00% GC)\n",
" median time: 131.392 ns (0.00% GC)\n",
" mean time: 131.440 ns (0.00% GC)\n",
" maximum time: 187.410 ns (0.00% GC)\n",
" --------------\n",
" samples: 10000\n",
" evals/sample: 886"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function test2(a,b)\n",
" a .+= 2 .* b\n",
"end\n",
"\n",
"a = zeros((N,))\n",
"b = ones((N,))\n",
"@benchmark test2(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "congressional-indie",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 0 bytes\n",
" allocs estimate: 0\n",
" --------------\n",
" minimum time: 128.999 ns (0.00% GC)\n",
" median time: 130.196 ns (0.00% GC)\n",
" mean time: 130.254 ns (0.00% GC)\n",
" maximum time: 157.804 ns (0.00% GC)\n",
" --------------\n",
" samples: 10000\n",
" evals/sample: 886"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function test3(a,b)\n",
" @. a += 2 * b\n",
"end\n",
"\n",
"a = zeros((N,))\n",
"b = ones((N,))\n",
"@benchmark test3(a, b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "viral-polish",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.6.0",
"language": "julia",
"name": "julia-1.6"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment