Created
December 15, 2019 16:26
-
-
Save antimon2/73c6010a34dc054b99ab4dd4d0fe8735 to your computer and use it in GitHub Desktop.
SimpleFuturePattern.jl
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": [ | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2019-12-15T16:04:29.887Z", | |
"end_time": "2019-12-16T01:04:34.215000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "versioninfo()", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Julia Version 1.3.0\nCommit 46ce4d7933 (2019-11-26 06:09 UTC)\nPlatform 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-6.0.1 (ORCJIT, skylake)\nEnvironment:\n JULIA_NUM_THREADS = 4\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2019-12-15T16:04:35.126Z", | |
"end_time": "2019-12-16T01:04:35.860000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Threads.nthreads()", | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 2, | |
"data": { | |
"text/plain": "4" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2019-12-15T16:04:36.705Z", | |
"end_time": "2019-12-16T01:04:37.198000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function fib_naive(n)\n if n ≤ 1\n n\n else\n fib_naive(n - 2) + fib_naive(n - 1)\n end\nend", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 3, | |
"data": { | |
"text/plain": "fib_naive (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2019-12-15T16:04:37.899Z", | |
"end_time": "2019-12-16T01:04:37.971000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "fib_naive(10)", | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 4, | |
"data": { | |
"text/plain": "55" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2019-12-15T16:04:39.248Z", | |
"end_time": "2019-12-16T01:04:40.064000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@time fib_naive(40)", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": " 0.726073 seconds (5 allocations: 176 bytes)\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 5, | |
"data": { | |
"text/plain": "102334155" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2019-12-15T16:04:47.541Z", | |
"end_time": "2019-12-16T01:04:47.717000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "fib_request(n; spawn=true) = Channel{Int}(spawn=spawn) do channel\n put!(channel, fib_naive(n))\nend", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 6, | |
"data": { | |
"text/plain": "fib_request (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2019-12-15T16:04:49.978Z", | |
"end_time": "2019-12-16T01:04:50.323000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "future = fib_request(40)", | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 7, | |
"data": { | |
"text/plain": "Channel{Int64}(sz_max:0,sz_curr:0)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2019-12-15T16:04:50.928Z", | |
"end_time": "2019-12-16T01:04:50.971000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "take!(future)", | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 8, | |
"data": { | |
"text/plain": "102334155" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2019-12-15T16:05:19.460Z", | |
"end_time": "2019-12-16T01:05:19.602000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function main(nrequests, nfib=40)\n requests = [fib_request(nfib) for _=1:nrequests]\n [take!(req) for req in requests]\nend", | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 9, | |
"data": { | |
"text/plain": "main (generic function with 2 methods)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2019-12-15T16:05:30.786Z", | |
"end_time": "2019-12-16T01:05:32.652000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "main(4)", | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 10, | |
"data": { | |
"text/plain": "4-element Array{Int64,1}:\n 102334155\n 102334155\n 102334155\n 102334155" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2019-12-15T16:05:43.069Z", | |
"end_time": "2019-12-16T01:05:43.860000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@time main(4)", | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": " 0.784660 seconds (148 allocations: 7.969 KiB)\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 11, | |
"data": { | |
"text/plain": "4-element Array{Int64,1}:\n 102334155\n 102334155\n 102334155\n 102334155" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2019-12-15T16:06:37.533Z", | |
"end_time": "2019-12-16T01:06:37.664000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function main2(nrequests, nfib=40)\n requests = [fib_request(nfib, spawn=false) for _=1:nrequests]\n [take!(req) for req in requests]\nend", | |
"execution_count": 12, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 12, | |
"data": { | |
"text/plain": "main2 (generic function with 2 methods)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2019-12-15T16:06:38.716Z", | |
"end_time": "2019-12-16T01:06:41.724000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "main2(4)", | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 13, | |
"data": { | |
"text/plain": "4-element Array{Int64,1}:\n 102334155\n 102334155\n 102334155\n 102334155" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2019-12-15T16:06:42.771Z", | |
"end_time": "2019-12-16T01:06:45.702000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@time main2(4)", | |
"execution_count": 14, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": " 2.925072 seconds (140 allocations: 7.813 KiB)\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 14, | |
"data": { | |
"text/plain": "4-element Array{Int64,1}:\n 102334155\n 102334155\n 102334155\n 102334155" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "julia-(4-threads)-1.3", | |
"display_name": "Julia (4 threads) 1.3.0", | |
"language": "julia" | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"name": "julia", | |
"mimetype": "application/julia", | |
"version": "1.3.0" | |
}, | |
"gist": { | |
"id": "", | |
"data": { | |
"description": "SimpleFuturePattern.jl", | |
"public": true | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment