Skip to content

Instantly share code, notes, and snippets.

@goropikari
Created October 1, 2018 21:20
Show Gist options
  • Select an option

  • Save goropikari/2970f3160b286c357caf768e0838e5a2 to your computer and use it in GitHub Desktop.

Select an option

Save goropikari/2970f3160b286c357caf768e0838e5a2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Julia Version 1.0.1\n",
"Commit 0d713926f8 (2018-09-29 19:05 UTC)\n",
"Platform Info:\n",
" OS: Linux (x86_64-pc-linux-gnu)\n",
" CPU: Intel(R) Core(TM) i5-4460T CPU @ 1.90GHz\n",
" WORD_SIZE: 64\n",
" LIBM: libopenlibm\n",
" LLVM: libLLVM-6.0.0 (ORCJIT, haswell)\n",
"Environment:\n",
" JULIA_SHELL = /usr/bin/zsh\n"
]
}
],
"source": [
"versioninfo()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"ispath(\"test\") || mkdir(\"test\")\n",
"cd(\"test\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"]activate ."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m registry at `~/.julia/registries/General`\n",
"\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m git-repo `https://github.com/JuliaRegistries/General.git`\n",
"\u001b[2K\u001b[?25h[1mFetching:\u001b[22m\u001b[39m [========================================>] 100.0 %.0 %\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n",
"\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/test/Project.toml`\n",
" \u001b[90m [c7e460c6]\u001b[39m\u001b[92m + ArgParse v0.6.0\u001b[39m\n",
" \u001b[90m [6e4b80f9]\u001b[39m\u001b[92m + BenchmarkTools v0.4.1\u001b[39m\n",
" \u001b[90m [9b87118b]\u001b[39m\u001b[92m + PackageCompiler v0.5.0\u001b[39m\n",
"\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/test/Manifest.toml`\n",
" \u001b[90m [c7e460c6]\u001b[39m\u001b[92m + ArgParse v0.6.0\u001b[39m\n",
" \u001b[90m [6e4b80f9]\u001b[39m\u001b[92m + BenchmarkTools v0.4.1\u001b[39m\n",
" \u001b[90m [9e28174c]\u001b[39m\u001b[92m + BinDeps v0.8.10\u001b[39m\n",
" \u001b[90m [b99e7846]\u001b[39m\u001b[92m + BinaryProvider v0.5.0\u001b[39m\n",
" \u001b[90m [e1450e63]\u001b[39m\u001b[92m + BufferedStreams v1.0.0\u001b[39m\n",
" \u001b[90m [34da2185]\u001b[39m\u001b[92m + Compat v1.2.0\u001b[39m\n",
" \u001b[90m [0862f596]\u001b[39m\u001b[92m + HTTPClient v0.2.1\u001b[39m\n",
" \u001b[90m [682c06a0]\u001b[39m\u001b[92m + JSON v0.19.0\u001b[39m\n",
" \u001b[90m [b27032c2]\u001b[39m\u001b[92m + LibCURL v0.4.1\u001b[39m\n",
" \u001b[90m [522f3ed2]\u001b[39m\u001b[92m + LibExpat v0.5.0\u001b[39m\n",
" \u001b[90m [2ec943e9]\u001b[39m\u001b[92m + Libz v1.0.0\u001b[39m\n",
" \u001b[90m [9b87118b]\u001b[39m\u001b[92m + PackageCompiler v0.5.0\u001b[39m\n",
" \u001b[90m [aa65fe97]\u001b[39m\u001b[92m + SnoopCompile v0.3.0\u001b[39m\n",
" \u001b[90m [b718987f]\u001b[39m\u001b[92m + TextWrap v0.3.0\u001b[39m\n",
" \u001b[90m [30578b45]\u001b[39m\u001b[92m + URIParser v0.4.0\u001b[39m\n",
" \u001b[90m [c17dfb99]\u001b[39m\u001b[92m + WinRPM v0.4.2\u001b[39m\n",
" \u001b[90m [2a0f44e3]\u001b[39m\u001b[92m + Base64 \u001b[39m\n",
" \u001b[90m [ade2ca70]\u001b[39m\u001b[92m + Dates \u001b[39m\n",
" \u001b[90m [8bb1440f]\u001b[39m\u001b[92m + DelimitedFiles \u001b[39m\n",
" \u001b[90m [8ba89e20]\u001b[39m\u001b[92m + Distributed \u001b[39m\n",
" \u001b[90m [b77e0a4c]\u001b[39m\u001b[92m + InteractiveUtils \u001b[39m\n",
" \u001b[90m [76f85450]\u001b[39m\u001b[92m + LibGit2 \u001b[39m\n",
" \u001b[90m [8f399da3]\u001b[39m\u001b[92m + Libdl \u001b[39m\n",
" \u001b[90m [37e2e46d]\u001b[39m\u001b[92m + LinearAlgebra \u001b[39m\n",
" \u001b[90m [56ddb016]\u001b[39m\u001b[92m + Logging \u001b[39m\n",
" \u001b[90m [d6f4376e]\u001b[39m\u001b[92m + Markdown \u001b[39m\n",
" \u001b[90m [a63ad114]\u001b[39m\u001b[92m + Mmap \u001b[39m\n",
" \u001b[90m [44cfe95a]\u001b[39m\u001b[92m + Pkg \u001b[39m\n",
" \u001b[90m [de0858da]\u001b[39m\u001b[92m + Printf \u001b[39m\n",
" \u001b[90m [3fa0cd96]\u001b[39m\u001b[92m + REPL \u001b[39m\n",
" \u001b[90m [9a3f8284]\u001b[39m\u001b[92m + Random \u001b[39m\n",
" \u001b[90m [ea8e919c]\u001b[39m\u001b[92m + SHA \u001b[39m\n",
" \u001b[90m [9e88b42a]\u001b[39m\u001b[92m + Serialization \u001b[39m\n",
" \u001b[90m [1a1011a3]\u001b[39m\u001b[92m + SharedArrays \u001b[39m\n",
" \u001b[90m [6462fe0b]\u001b[39m\u001b[92m + Sockets \u001b[39m\n",
" \u001b[90m [2f01184e]\u001b[39m\u001b[92m + SparseArrays \u001b[39m\n",
" \u001b[90m [10745b16]\u001b[39m\u001b[92m + Statistics \u001b[39m\n",
" \u001b[90m [8dfed614]\u001b[39m\u001b[92m + Test \u001b[39m\n",
" \u001b[90m [cf7118a7]\u001b[39m\u001b[92m + UUIDs \u001b[39m\n",
" \u001b[90m [4ec0a83e]\u001b[39m\u001b[92m + Unicode \u001b[39m\n"
]
}
],
"source": [
"]add ArgParse PackageCompiler BenchmarkTools"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"using PackageCompiler, BenchmarkTools, Random"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"fib(n::Int) = n < 2 ? n : fib(n-1) + fib(n-2)\n",
"open(\"fib.jl\", \"w\") do file\n",
" print(file, \"\"\"module Hello\n",
"fib(n::Int) = n < 2 ? n : fib(n-1) + fib(n-2)\n",
"Base.@ccallable function julia_main(ARGS::Vector{String})::Cint\n",
" fib(20)\n",
" return 0\n",
"end\n",
"\n",
"end # module\n",
"\"\"\")\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Julia program file:\n",
" \"/home/arch/test/fib.jl\"\n",
"C program file:\n",
" \"/home/arch/.julia/packages/PackageCompiler/BBtC7/examples/program.c\"\n",
"Build directory:\n",
" \"/home/arch/test/fibdir\"\n",
"All done\n",
"135.090758 seconds (1.79 M allocations: 87.810 MiB, 0.03% gc time)\n"
]
}
],
"source": [
"@time build_executable(\"fib.jl\", \"fib\", builddir=\"fibdir\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 0 bytes\n",
" allocs estimate: 0\n",
" --------------\n",
" minimum time: 42.443 μs (0.00% GC)\n",
" median time: 42.465 μs (0.00% GC)\n",
" mean time: 43.317 μs (0.00% GC)\n",
" maximum time: 85.702 μs (0.00% GC)\n",
" --------------\n",
" samples: 10000\n",
" evals/sample: 1"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mpfib = @benchmark fib(20)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 1.14 KiB\n",
" allocs estimate: 25\n",
" --------------\n",
" minimum time: 178.085 ms (0.00% GC)\n",
" median time: 181.576 ms (0.00% GC)\n",
" mean time: 184.143 ms (0.00% GC)\n",
" maximum time: 211.511 ms (0.00% GC)\n",
" --------------\n",
" samples: 28\n",
" evals/sample: 1"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mcfib = @benchmark run(`fibdir/fib`)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.TrialRatio: \n",
" time: 4275.90226068527\n",
" gctime: 1.0\n",
" memory: Inf\n",
" allocs: Inf"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ratio(BenchmarkTools.median(mcfib), BenchmarkTools.median(mpfib)) # mcfib / mpfib"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"matrixmul(n::Int) = rand(n,n) * rand(n,n)\n",
"open(\"matrixmul.jl\", \"w\") do file\n",
" print(file, \"\"\"module Hello\n",
"using Random\n",
"Base.@ccallable function julia_main(ARGS::Vector{String})::Cint\n",
" Random.seed!(0)\n",
" n = 1000\n",
" rand(n,n) * rand(n,n)\n",
" return 0\n",
"end\n",
"\n",
"end # module\n",
"\"\"\")\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Julia program file:\n",
" \"/home/arch/test/matrixmul.jl\"\n",
"C program file:\n",
" \"/home/arch/.julia/packages/PackageCompiler/BBtC7/examples/program.c\"\n",
"Build directory:\n",
" \"/home/arch/test/matrixmuldir\"\n",
"All done\n",
"130.314236 seconds (10.69 k allocations: 500.266 KiB)\n"
]
}
],
"source": [
"@time build_executable(\"matrixmul.jl\", \"matrixmul\", builddir=\"matrixmuldir\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 22.89 MiB\n",
" allocs estimate: 8\n",
" --------------\n",
" minimum time: 27.088 ms (0.00% GC)\n",
" median time: 28.026 ms (2.88% GC)\n",
" mean time: 29.798 ms (6.41% GC)\n",
" maximum time: 125.353 ms (73.64% GC)\n",
" --------------\n",
" samples: 168\n",
" evals/sample: 1"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mpmat = @benchmark begin \n",
" Random.seed!(0)\n",
" matrixmul(1000)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 1.19 KiB\n",
" allocs estimate: 27\n",
" --------------\n",
" minimum time: 216.484 ms (0.00% GC)\n",
" median time: 219.590 ms (0.00% GC)\n",
" mean time: 221.097 ms (0.00% GC)\n",
" maximum time: 229.687 ms (0.00% GC)\n",
" --------------\n",
" samples: 23\n",
" evals/sample: 1"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mcmat = @benchmark run(`matrixmuldir/matrixmul`)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.TrialRatio: \n",
" time: 7.835319509919899\n",
" gctime: 0.0\n",
" memory: 5.0665923566454356e-5\n",
" allocs: 3.375"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ratio(BenchmarkTools.median(mcmat), BenchmarkTools.median(mpmat)) # mcmat / mpmat"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"open(\"sin.jl\", \"w\") do file\n",
" print(file, \"\"\"module Hello\n",
"Base.@ccallable function julia_main(ARGS::Vector{String})::Cint\n",
" sin(1.0)\n",
" return 0\n",
"end\n",
"\n",
"end # module\n",
"\"\"\")\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Julia program file:\n",
" \"/home/arch/test/sin.jl\"\n",
"C program file:\n",
" \"/home/arch/.julia/packages/PackageCompiler/BBtC7/examples/program.c\"\n",
"Build directory:\n",
" \"/home/arch/test/sindir\"\n",
"All done\n",
"145.151027 seconds (11.00 k allocations: 509.703 KiB)\n"
]
}
],
"source": [
"@time build_executable(\"sin.jl\", \"sin\", builddir=\"sindir\")"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 0 bytes\n",
" allocs estimate: 0\n",
" --------------\n",
" minimum time: 11.542 ns (0.00% GC)\n",
" median time: 11.570 ns (0.00% GC)\n",
" mean time: 11.968 ns (0.00% GC)\n",
" maximum time: 67.348 ns (0.00% GC)\n",
" --------------\n",
" samples: 10000\n",
" evals/sample: 999"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mpsin = @benchmark sin(1.0)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 1.14 KiB\n",
" allocs estimate: 25\n",
" --------------\n",
" minimum time: 184.560 ms (0.00% GC)\n",
" median time: 188.123 ms (0.00% GC)\n",
" mean time: 188.845 ms (0.00% GC)\n",
" maximum time: 196.590 ms (0.00% GC)\n",
" --------------\n",
" samples: 27\n",
" evals/sample: 1"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mcsin = @benchmark run(`sindir/sin`)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.TrialRatio: \n",
" time: 1.6260187889340715e7\n",
" gctime: 1.0\n",
" memory: Inf\n",
" allocs: Inf"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ratio(BenchmarkTools.median(mcsin), BenchmarkTools.median(mpsin)) # mcsin / mpsin"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.0.1",
"language": "julia",
"name": "julia-1.0"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.0.1"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"toc_cell": false,
"toc_position": {},
"toc_section_display": "block",
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment