目的:
- 現時点では、効率的な並列プログラムを書くには、基盤となるマシンアーキテクチャを把握している必要がある
- この章の目的は、
- アーキテクチャが、並列プログラミングの実行性能にどのような影響を与えるかを理解する
- そのうえで効率的なプログラムの書き方を学ぶ
- 皆がなじみのある相互排他(ロック)の現在のマルチプロセッサ上での効率的な実装の話から始める
| -module(json_decode_1). | |
| -export([decode/1]). | |
| -type json_value() :: null | boolean() | json_number() | | |
| json_string() | json_array() | json_object(). | |
| -type json_number() :: non_neg_integer(). | |
| -type json_string() :: binary(). | |
| -type json_array() :: [json_value()]. | |
| -type json_object() :: {[json_object_member()]}. |
| $ erl | |
| Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:2:2] [async-threads:10] [hipe] | |
| Eshell V5.10.4 (abort with ^G) | |
| %% nativeをコンパイルオプションに、指定するとHiPEコンパイラが使われる | |
| > c(hoge, [native]). | |
| {ok, hoge}. | |
| %% hipe:help/0 でヘルプが表示できる | |
| > hipe:help(). |
| { | |
| "id": 1, | |
| "jsonrpc": "2.0", | |
| "total": 1, | |
| "result": [ | |
| { | |
| "id": 1, | |
| "avatar": "images/user_1.png", | |
| "age": 38, | |
| "admin": false, |
| -module(bench). | |
| -export([bench/0]). | |
| -spec bench() -> [Result] when | |
| Result :: {module(), ElementSize::pos_integer(), InsertAverageNanoSeconds::non_neg_integer()}. | |
| bench() -> | |
| ElementSizeList = [1, 10, 100, 1000, 10000, 100000], | |
| InputList = [shuffle(lists:seq(0, Size - 1)) || Size <- ElementSizeList], |
| // g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4) | |
| #include <iostream> | |
| #include <cstdlib> | |
| unsigned fib(unsigned n) { | |
| if (n < 2) { | |
| return n; | |
| } else { | |
| return fib(n - 2) + fib(n - 1); |