WASMで色々やるためにRustを勉強している。↓の本の評判がいいので読む。
3章 クイックツアー
バイトニックソートを Rust 上で実装する。並列処理に向いたアルゴリズムなので Rust 向きらしい。下の順に進めていくそうだ。
TIPS: /proc/cpuinfo
でCPUのコア数が確認できる。自宅のノートは8コアだった。
$ egrep 'processor|model name' /proc/cpuinfo processor : 0 model name : Intel(R) Core(TM) i5-10210U CPU @ 1.60GHz . . processor : 7 model name : Intel(R) Core(TM) i5-10210U CPU @ 1.60GHz
サンプルコードの実行とベンチマーク
書籍のサンプルコードを https://github.com/ghmagazine/rustbook からダウンロードしてビルド。
$ git clone https://github.com/ghmagazine/rustbook $ cd rustbook/ch03/bitonic-sorter $ cargo run --release --example benchmark -- 26 # 29は自分のマシンだと時間かかりすぎた Finished release [optimized] target(s) in 0.29s Running `target/release/examples/benchmark 26` sorting 67108864 integers (256.0 MB) cpu info: 4 physical cores, 8 logical cores seq_sort: sorted 67108864 integers in 64.097529 seconds par_sort: sorted 67108864 integers in 17.792371 seconds speed up: 3.60x
サンプルコードのビルドと実行まではできた。ちゃんと並列ソートの方が高速になっている。
次は実際にコードを書いていく。 実践Rust入門を読んでいる(2) - bitonic-sorterの実装