Skip to content

Instantly share code, notes, and snippets.

@aviatesk
Created September 1, 2019 09:22
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 aviatesk/a606461101f955c7d6b98c706c5d85c9 to your computer and use it in GitHub Desktop.
Save aviatesk/a606461101f955c7d6b98c706c5d85c9 to your computer and use it in GitHub Desktop.
Benchmarks for `@nospecialize`
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Benchmark for `@nospecialize`\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"inputHidden": false,
"outputHidden": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0.000203 seconds (21 allocations: 880 bytes)\n",
" 0.063754 seconds (3.41 k allocations: 234.297 KiB)\n",
" 0.000062 seconds (15 allocations: 448 bytes)\n"
]
}
],
"source": [
"# setups\n",
"function nospecialize(@nospecialize(x))\n",
" \"I'm code without need to specialize on a given argument\"\n",
"end\n",
"function specialize(x)\n",
" \"I'm code with need to specialize on a given argument\"\n",
"end\n",
"xs = (1, 1., 1//1, 1 + 0im, true, \"1\", '1', s\"1\", r\"1\")\n",
"\n",
"# precompile for `Nothing`\n",
"nospecialize(nothing)\n",
"specialize(nothing)\n",
"\n",
"# benchmarks\n",
"@time for x = xs\n",
" nospecialize(x)\n",
"end\n",
"@time for x = xs\n",
" specialize(x)\n",
"end\n",
"@time for x = xs\n",
" specialize(x)\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"see how `Number`s are treated\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"inputHidden": false,
"outputHidden": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0.000069 seconds (12 allocations: 352 bytes)\n",
" 0.037086 seconds (2.27 k allocations: 156.250 KiB)\n",
" 0.000058 seconds (12 allocations: 352 bytes)\n",
"isabstracttype(Number) = true\n"
]
},
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# setups\n",
"function nospecializeonnumber(@nospecialize(n::Number))\n",
" \"I'm code who doesn't specialize on a given number\"\n",
"end\n",
"function specializeonnumber(n::Number)\n",
" \"I'm code who does specialize on a given number\"\n",
"end\n",
"ns = (1, BigInt(1), 1., BigFloat(1), 1//1, 1 + 0im)\n",
"\n",
"# precompile for `Bool`\n",
"nospecializeonnumber(true)\n",
"specializeonnumber(true)\n",
"\n",
"# benchmarks\n",
"@time for n = ns\n",
" nospecializeonnumber(n)\n",
"end\n",
"@time for n = ns\n",
" specializeonnumber(n)\n",
"end\n",
"@time for n = ns\n",
" specializeonnumber(n)\n",
"end\n",
"\n",
"@show isabstracttype(Number)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"see how funcitons are treated\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"inputHidden": false,
"outputHidden": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0.001125 seconds (36 allocations: 1.875 KiB)\n",
" 0.000092 seconds (12 allocations: 192 bytes)\n",
" 0.000078 seconds (12 allocations: 192 bytes)\n",
"isabstracttype(Function) = true\n"
]
},
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# setups\n",
"function nospecializeonfunc(@nospecialize(f::Function))\n",
" \"I'm code who doesn't specialize on the given function\"\n",
"end\n",
"function specializeonfunc(f::Function)\n",
" \"I'm code who does specialize on the given function\"\n",
"end\n",
"fs = (sin, cos, tan, asin, acos, atan, asinh, acosh, atanh, asind, acosd, atand)\n",
"\n",
"# precompile for `print`\n",
"nospecializeonfunc(print)\n",
"specializeonfunc(print)\n",
"\n",
"# benchmarks\n",
"@time for f = fs\n",
" nospecializeonfunc(f)\n",
"end\n",
"@time for f = fs\n",
" specializeonfunc(f)\n",
"end\n",
"@time for f = fs\n",
" specializeonfunc(f)\n",
"end\n",
"\n",
"@show isabstracttype(Function)"
]
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"kernelspec": {
"display_name": "Julia 1.2.0",
"language": "julia",
"name": "julia-1.2"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.2.0"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment