Created
October 29, 2020 07:25
-
-
Save mi2428/3606db4e7e3c65b246acb686302bec93 to your computer and use it in GitHub Desktop.
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": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import Pkg; Pkg.add(\"BenchmarkTools\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"dist (generic function with 2 methods)" | |
] | |
}, | |
"execution_count": 17, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"mutable struct Vector\n", | |
" x::Float32\n", | |
" y::Float32\n", | |
"end\n", | |
"\n", | |
"l2norm(v::Vector) = sqrt(v.x^2 + v.y^2)\n", | |
"dist(v1::Vector, v2::Vector) = l2norm(Vector(v1.x - v2.x, v1.y - v2.y))\n", | |
"\n", | |
"l2norm(v::Array{Float32}) = sqrt(v[1]^2 + v[2]^2)\n", | |
"dist(v1::Array{Float32}, v2::Array{Float32}) = l2norm([v1[1] - v2[1], v1[2] - v2[2]])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 2.75 MiB\n", | |
" allocs estimate: 30000\n", | |
" --------------\n", | |
" minimum time: 1.203 ms (0.00% GC)\n", | |
" median time: 1.244 ms (0.00% GC)\n", | |
" mean time: 1.436 ms (11.91% GC)\n", | |
" maximum time: 5.245 ms (51.63% GC)\n", | |
" --------------\n", | |
" samples: 3480\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"using BenchmarkTools\n", | |
"\n", | |
"function vectorloop(max)\n", | |
" for i = 1:max\n", | |
" v1 = Vector(rand() * 100, rand() * 100)\n", | |
" v2 = Vector(rand() * 100, rand() * 100)\n", | |
" dist(v1, v2)\n", | |
" end\n", | |
"end\n", | |
"\n", | |
"@benchmark arrayloop(10000)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 2.75 MiB\n", | |
" allocs estimate: 30000\n", | |
" --------------\n", | |
" minimum time: 1.211 ms (0.00% GC)\n", | |
" median time: 1.243 ms (0.00% GC)\n", | |
" mean time: 1.438 ms (12.18% GC)\n", | |
" maximum time: 5.746 ms (60.34% GC)\n", | |
" --------------\n", | |
" samples: 3477\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 24, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"using BenchmarkTools\n", | |
"\n", | |
"function arrayloop(max)\n", | |
" for i = 1:max\n", | |
" v1 = [Float32(rand() * 100), Float32(rand() * 100)]\n", | |
" v2 = [Float32(rand() * 100), Float32(rand() * 100)]\n", | |
" dist(v1, v2)\n", | |
" end\n", | |
"end\n", | |
"\n", | |
"@benchmark arrayloop(10000)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Julia 1.5.1", | |
"language": "julia", | |
"name": "julia-1.5" | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"mimetype": "application/julia", | |
"name": "julia", | |
"version": "1.5.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment