Skip to content

Instantly share code, notes, and snippets.

@antimon2
Last active May 31, 2021 14:35
Show Gist options
  • Save antimon2/88870dc9864957429886d794addb8653 to your computer and use it in GitHub Desktop.
Save antimon2/88870dc9864957429886d794addb8653 to your computer and use it in GitHub Desktop.
UnzipSample.jl.ipynb
[compat]
julia = "1.6"
[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Unzip = "41fe7b60-77ed-43a1-b4f0-825fd5a5650d"
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:51.623000+09:00",
"start_time": "2021-05-31T14:25:52.225Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Julia Version 1.6.1\n",
"Commit 6aaedecc44 (2021-04-23 05:59 UTC)\n",
"Platform Info:\n",
" OS: Linux (x86_64-pc-linux-gnu)\n",
" CPU: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz\n",
" WORD_SIZE: 64\n",
" LIBM: libopenlibm\n",
" LLVM: libLLVM-11.0.1 (ORCJIT, skylake)\n"
]
}
],
"source": [
"versioninfo()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:52.176000+09:00",
"start_time": "2021-05-31T14:25:52.226Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"p (generic function with 1 method)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p(t) = (cos(t), sin(t))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:52.488000+09:00",
"start_time": "2021-05-31T14:25:52.227Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0.0:0.5:6.0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t_range = 0:0.5:2π"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:53.707000+09:00",
"start_time": "2021-05-31T14:25:52.228Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"13-element Vector{Tuple{Float64, Float64}}:\n",
" (1.0, 0.0)\n",
" (0.8775825618903728, 0.479425538604203)\n",
" (0.5403023058681398, 0.8414709848078965)\n",
" (0.0707372016677029, 0.9974949866040544)\n",
" (-0.4161468365471424, 0.9092974268256817)\n",
" (-0.8011436155469337, 0.5984721441039564)\n",
" (-0.9899924966004454, 0.1411200080598672)\n",
" (-0.9364566872907963, -0.35078322768961984)\n",
" (-0.6536436208636119, -0.7568024953079282)\n",
" (-0.2107957994307797, -0.977530117665097)\n",
" (0.28366218546322625, -0.9589242746631385)\n",
" (0.70866977429126, -0.7055403255703919)\n",
" (0.960170286650366, -0.27941549819892586)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p.(t_range)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:54.130000+09:00",
"start_time": "2021-05-31T14:25:52.229Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"zip((1.0, 0.0), (0.8775825618903728, 0.479425538604203), (0.5403023058681398, 0.8414709848078965), (0.0707372016677029, 0.9974949866040544), (-0.4161468365471424, 0.9092974268256817), (-0.8011436155469337, 0.5984721441039564), (-0.9899924966004454, 0.1411200080598672), (-0.9364566872907963, -0.35078322768961984), (-0.6536436208636119, -0.7568024953079282), (-0.2107957994307797, -0.977530117665097), (0.28366218546322625, -0.9589242746631385), (0.70866977429126, -0.7055403255703919), (0.960170286650366, -0.27941549819892586))"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs, ys = zip((p(t) for t in t_range)...)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:54.402000+09:00",
"start_time": "2021-05-31T14:25:52.231Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(1.0, 0.8775825618903728, 0.5403023058681398, 0.0707372016677029, -0.4161468365471424, -0.8011436155469337, -0.9899924966004454, -0.9364566872907963, -0.6536436208636119, -0.2107957994307797, 0.28366218546322625, 0.70866977429126, 0.960170286650366)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:54.402000+09:00",
"start_time": "2021-05-31T14:25:52.233Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(0.0, 0.479425538604203, 0.8414709848078965, 0.9974949866040544, 0.9092974268256817, 0.5984721441039564, 0.1411200080598672, -0.35078322768961984, -0.7568024953079282, -0.977530117665097, -0.9589242746631385, -0.7055403255703919, -0.27941549819892586)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ys"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:54.471000+09:00",
"start_time": "2021-05-31T14:25:52.234Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"unzip1 (generic function with 1 method)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"unzip1(iter) = zip(iter...)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:54.477000+09:00",
"start_time": "2021-05-31T14:25:52.235Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"zip((1.0, 0.0), (0.8775825618903728, 0.479425538604203), (0.5403023058681398, 0.8414709848078965), (0.0707372016677029, 0.9974949866040544), (-0.4161468365471424, 0.9092974268256817), (-0.8011436155469337, 0.5984721441039564), (-0.9899924966004454, 0.1411200080598672), (-0.9364566872907963, -0.35078322768961984), (-0.6536436208636119, -0.7568024953079282), (-0.2107957994307797, -0.977530117665097), (0.28366218546322625, -0.9589242746631385), (0.70866977429126, -0.7055403255703919), (0.960170286650366, -0.27941549819892586))"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs1, ys1 = unzip1(p(t) for t in t_range)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:54.728000+09:00",
"start_time": "2021-05-31T14:25:52.236Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs1 == xs"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:54.729000+09:00",
"start_time": "2021-05-31T14:25:52.237Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ys1 == ys"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:55.364000+09:00",
"start_time": "2021-05-31T14:25:52.239Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2×13 reinterpret(reshape, Float64, ::Vector{Tuple{Float64, Float64}}) with eltype Float64:\n",
" 1.0 0.877583 0.540302 0.0707372 … 0.283662 0.70867 0.96017\n",
" 0.0 0.479426 0.841471 0.997495 -0.958924 -0.70554 -0.279415"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"reinterpret(reshape, eltype(p(t_range[begin])), p.(t_range))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:55.713000+09:00",
"start_time": "2021-05-31T14:25:52.240Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Base.Generator{Base.OneTo{Int64}, Base.var\"#191#192\"{Base.ReinterpretArray{Float64, 2, Tuple{Float64, Float64}, Vector{Tuple{Float64, Float64}}, true}}}(Base.var\"#191#192\"{Base.ReinterpretArray{Float64, 2, Tuple{Float64, Float64}, Vector{Tuple{Float64, Float64}}, true}}([1.0 0.8775825618903728 … 0.70866977429126 0.960170286650366; 0.0 0.479425538604203 … -0.7055403255703919 -0.27941549819892586]), Base.OneTo(2))"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs, ys = eachrow(reinterpret(reshape, eltype(p(t_range[begin])), p.(t_range)))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:56.246000+09:00",
"start_time": "2021-05-31T14:25:52.241Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"13-element view(reinterpret(reshape, Float64, ::Vector{Tuple{Float64, Float64}}), 1, :) with eltype Float64:\n",
" 1.0\n",
" 0.8775825618903728\n",
" 0.5403023058681398\n",
" 0.0707372016677029\n",
" -0.4161468365471424\n",
" -0.8011436155469337\n",
" -0.9899924966004454\n",
" -0.9364566872907963\n",
" -0.6536436208636119\n",
" -0.2107957994307797\n",
" 0.28366218546322625\n",
" 0.70866977429126\n",
" 0.960170286650366"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:56.247000+09:00",
"start_time": "2021-05-31T14:25:52.242Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"13-element view(reinterpret(reshape, Float64, ::Vector{Tuple{Float64, Float64}}), 2, :) with eltype Float64:\n",
" 0.0\n",
" 0.479425538604203\n",
" 0.8414709848078965\n",
" 0.9974949866040544\n",
" 0.9092974268256817\n",
" 0.5984721441039564\n",
" 0.1411200080598672\n",
" -0.35078322768961984\n",
" -0.7568024953079282\n",
" -0.977530117665097\n",
" -0.9589242746631385\n",
" -0.7055403255703919\n",
" -0.27941549819892586"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ys"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:56.328000+09:00",
"start_time": "2021-05-31T14:25:52.243Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"unzip2 (generic function with 1 method)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"unzip2(iter) = eachrow(reinterpret(reshape, eltype(eltype(iter)), iter))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:56.354000+09:00",
"start_time": "2021-05-31T14:25:52.244Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Base.Generator{Base.OneTo{Int64}, Base.var\"#191#192\"{Base.ReinterpretArray{Float64, 2, Tuple{Float64, Float64}, Vector{Tuple{Float64, Float64}}, true}}}(Base.var\"#191#192\"{Base.ReinterpretArray{Float64, 2, Tuple{Float64, Float64}, Vector{Tuple{Float64, Float64}}, true}}([1.0 0.8775825618903728 … 0.70866977429126 0.960170286650366; 0.0 0.479425538604203 … -0.7055403255703919 -0.27941549819892586]), Base.OneTo(2))"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs2, ys2 = unzip2([p(t) for t in t_range])"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:56.383000+09:00",
"start_time": "2021-05-31T14:25:52.245Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs2 == xs && ys2 == ys"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:56.387000+09:00",
"start_time": "2021-05-31T14:25:52.246Z"
}
},
"outputs": [],
"source": [
"using BenchmarkTools"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:58.972000+09:00",
"start_time": "2021-05-31T14:25:52.247Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 1.67 KiB\n",
" allocs estimate: 19\n",
" --------------\n",
" minimum time: 856.389 ns (0.00% GC)\n",
" median time: 892.611 ns (0.00% GC)\n",
" mean time: 996.744 ns (6.83% GC)\n",
" maximum time: 44.123 μs (97.66% GC)\n",
" --------------\n",
" samples: 10000\n",
" evals/sample: 54"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@benchmark begin\n",
" xs, ys = unzip1([p(t) for t in $t_range])\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:26:00.427000+09:00",
"start_time": "2021-05-31T14:25:52.248Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 1.86 KiB\n",
" allocs estimate: 32\n",
" --------------\n",
" minimum time: 1.259 μs (0.00% GC)\n",
" median time: 1.324 μs (0.00% GC)\n",
" mean time: 1.519 μs (5.22% GC)\n",
" maximum time: 224.547 μs (99.14% GC)\n",
" --------------\n",
" samples: 10000\n",
" evals/sample: 10"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@benchmark begin\n",
" xs, ys = unzip1(p(t) for t in $t_range)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:26:02.255000+09:00",
"start_time": "2021-05-31T14:25:52.249Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 288 bytes\n",
" allocs estimate: 1\n",
" --------------\n",
" minimum time: 214.255 ns (0.00% GC)\n",
" median time: 223.759 ns (0.00% GC)\n",
" mean time: 237.348 ns (1.87% GC)\n",
" maximum time: 1.933 μs (80.85% GC)\n",
" --------------\n",
" samples: 10000\n",
" evals/sample: 506"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@benchmark begin\n",
" xs, ys = unzip2([p(t) for t in $t_range])\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:25:19.567000+09:00",
"start_time": "2021-05-31T14:25:05.972Z"
}
},
"outputs": [],
"source": [
"# ]add Unzip"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:26:05.040000+09:00",
"start_time": "2021-05-31T14:26:06.938Z"
}
},
"outputs": [],
"source": [
"using Unzip"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:26:32.853000+09:00",
"start_time": "2021-05-31T14:26:34.332Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"([1.0, 0.8775825618903728, 0.5403023058681398, 0.0707372016677029, -0.4161468365471424, -0.8011436155469337, -0.9899924966004454, -0.9364566872907963, -0.6536436208636119, -0.2107957994307797, 0.28366218546322625, 0.70866977429126, 0.960170286650366], [0.0, 0.479425538604203, 0.8414709848078965, 0.9974949866040544, 0.9092974268256817, 0.5984721441039564, 0.1411200080598672, -0.35078322768961984, -0.7568024953079282, -0.977530117665097, -0.9589242746631385, -0.7055403255703919, -0.27941549819892586])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs, ys = unzip(p(t) for t in t_range)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:26:48.847000+09:00",
"start_time": "2021-05-31T14:26:50.720Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs == xs2 && ys == ys2"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:27:07.644000+09:00",
"start_time": "2021-05-31T14:27:07.994Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 672 bytes\n",
" allocs estimate: 3\n",
" --------------\n",
" minimum time: 305.801 ns (0.00% GC)\n",
" median time: 318.325 ns (0.00% GC)\n",
" mean time: 340.916 ns (2.60% GC)\n",
" maximum time: 3.796 μs (91.16% GC)\n",
" --------------\n",
" samples: 10000\n",
" evals/sample: 251"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@benchmark begin\n",
" xs, ys = unzip([p(t) for t in $t_range])\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-31T23:27:09.298000+09:00",
"start_time": "2021-05-31T14:27:07.995Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 384 bytes\n",
" allocs estimate: 2\n",
" --------------\n",
" minimum time: 245.917 ns (0.00% GC)\n",
" median time: 260.645 ns (0.00% GC)\n",
" mean time: 274.998 ns (1.61% GC)\n",
" maximum time: 2.238 μs (87.53% GC)\n",
" --------------\n",
" samples: 10000\n",
" evals/sample: 372"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@benchmark begin\n",
" xs, ys = unzip(p(t) for t in $t_range)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"_draft": {
"nbviewer_url": "https://gist.github.com/88870dc9864957429886d794addb8653"
},
"gist": {
"data": {
"description": "UnzipSample.jl.ipynb",
"public": true
},
"id": "88870dc9864957429886d794addb8653"
},
"kernelspec": {
"display_name": "Julia 1.6.1",
"language": "julia",
"name": "julia-1.6"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment