Skip to content

Instantly share code, notes, and snippets.

@ceptreee
Created January 8, 2018 20:15
Show Gist options
  • Save ceptreee/2205a1e0da691bc815fbae2a945b2288 to your computer and use it in GitHub Desktop.
Save ceptreee/2205a1e0da691bc815fbae2a945b2288 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"using BenchmarkTools"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Julia Version 0.6.2\n",
"Commit d386e40c17 (2017-12-13 18:08 UTC)\n",
"Platform Info:\n",
" OS: macOS (x86_64-apple-darwin14.5.0)\n",
" CPU: Intel(R) Core(TM) i7-3740QM CPU @ 2.70GHz\n",
" WORD_SIZE: 64\n",
" BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)\n",
" LAPACK: libopenblas64_\n",
" LIBM: libopenlibm\n",
" LLVM: libLLVM-3.9.1 (ORCJIT, ivybridge)\n"
]
}
],
"source": [
"versioninfo()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"x = ones(100000)\n",
"y = ones(100000);"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dot_fast_inbounds (generic function with 1 method)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function dot_fast_inbounds(a, b)\n",
" s = zero(eltype(a))\n",
" for i in 1:endof(a)\n",
" @fastmath @inbounds s += a[i] * b[i]\n",
" end\n",
" return s\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dot_fast (generic function with 1 method)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function dot_fast(a, b)\n",
" s = zero(eltype(a))\n",
" for i in 1:endof(a)\n",
" @fastmath s += a[i] * b[i]\n",
" end\n",
" return s\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dot_inbounds (generic function with 1 method)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function dot_inbounds(a, b)\n",
" s = zero(eltype(a))\n",
" for i in 1:endof(a)\n",
" @inbounds s += a[i] * b[i]\n",
" end\n",
" return s\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dot2 (generic function with 1 method)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function dot2(a, b)\n",
" s = zero(eltype(a))\n",
" for i in 1:endof(a)\n",
" s += a[i] * b[i]\n",
" end\n",
" return s\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 16 bytes\n",
" allocs estimate: 1\n",
" --------------\n",
" minimum time: 81.781 μs (0.00% GC)\n",
" median time: 86.587 μs (0.00% GC)\n",
" mean time: 91.183 μs (0.00% GC)\n",
" maximum time: 256.500 μs (0.00% GC)\n",
" --------------\n",
" samples: 10000\n",
" evals/sample: 1"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@benchmark dot2(x,y)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 16 bytes\n",
" allocs estimate: 1\n",
" --------------\n",
" minimum time: 43.266 μs (0.00% GC)\n",
" median time: 45.942 μs (0.00% GC)\n",
" mean time: 48.089 μs (0.00% GC)\n",
" maximum time: 307.781 μs (0.00% GC)\n",
" --------------\n",
" samples: 10000\n",
" evals/sample: 1"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@benchmark dot_fast_inbounds(x,y)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 16 bytes\n",
" allocs estimate: 1\n",
" --------------\n",
" minimum time: 81.746 μs (0.00% GC)\n",
" median time: 81.950 μs (0.00% GC)\n",
" mean time: 88.553 μs (0.00% GC)\n",
" maximum time: 256.499 μs (0.00% GC)\n",
" --------------\n",
" samples: 10000\n",
" evals/sample: 1"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@benchmark dot_fast(x,y)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 16 bytes\n",
" allocs estimate: 1\n",
" --------------\n",
" minimum time: 81.461 μs (0.00% GC)\n",
" median time: 81.555 μs (0.00% GC)\n",
" mean time: 83.597 μs (0.00% GC)\n",
" maximum time: 281.873 μs (0.00% GC)\n",
" --------------\n",
" samples: 10000\n",
" evals/sample: 1"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@benchmark dot_inbounds(x,y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.6.2",
"language": "julia",
"name": "julia-0.6"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment