-
Rust❤️rayon과 일반 기능{into_par_iter() vs into_iter()}❤️의 차이는 얼마나 날까?(concurrency의 대단함코딩Coding/Rust❤️Optimization❤️ 2022. 6. 14. 07:56728x90
into_par_iter() vs into_iter()} 속도 ❤️의 차이
역시 병렬 실행이 3배 이상 빠르다 ㅋ
32분 15초 부터
https://youtu.be/9QyJ6b5I274
rayon과 일반 기능의 차이는 얼마나 날까?
// rayon과 일반 기능의 차이는 얼마나 날까? use rayon::prelude::*; use std::time::Instant; fn main() { let time_1 = Instant::now(); let async_vec: Vec<_> = (0..1_000_000).into_par_iter().collect(); let time_1_ela = time_1.elapsed(); let time_2 = Instant::now(); let sync_vec: Vec<_> = (0..1_000_000).into_iter().collect(); let time_2_ela = time_2.elapsed(); // for i in async_vec.iter() { // println!("Element : {:?}", i); // } println!("async_vec time__rayon : {:?}", time_1_ela); println!("sync_vec time : {:?}", time_2_ela); }
결과cargo run Compiling rust_polyglot v0.1.0 (/Users/globalyoung/Documents/Project/Github/rust_project/rust_polyglot) warning: unused variable: `async_vec` --> src/main.rs:6:9 | 6 | let async_vec: Vec<_> = (0..1_000_000).into_par_iter().collect(); | ^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_async_vec` | = note: `#[warn(unused_variables)]` on by default warning: unused variable: `sync_vec` --> src/main.rs:10:9 | 10 | let sync_vec: Vec<_> = (0..1_000_000).into_iter().collect(); | ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_sync_vec` warning: `rust_polyglot` (bin "rust_polyglot") generated 2 warnings Finished dev [unoptimized + debuginfo] target(s) in 0.16s Running `target/debug/rust_polyglot` async_vec time__rayon : 4.197ms sync_vec time : 15.90325ms
시간이 무려 3배 차이 난다.
이래서 동시 실행 , 병렬 코딩을 배워야한다. ㅋㅋㅋ
굿 Concurrency 만세!! 👍 ❤️
예문은 공식 문서 참고- collect에 있다.
https://docs.rs/rayon/1.2.0/rayon/iter/trait.ParallelIterator.htmlrayon::iter::ParallelIterator - Rust
pub trait ParallelIterator: Sized + Send { type Item: Send; fn drive_unindexed (self, consumer: C) -> C::Result where C: UnindexedConsumer ; fn for_each (self, op: OP) where OP: Fn(Self::Item) + Sync + Send, { ...
docs.rs
병렬 실행
into_par_iter()
https://docs.rs/rayon/1.5.3/rayon/iter/trait.IndexedParallelIterator.html#method.zipIndexedParallelIterator in rayon::iter - Rust
Iterates over tuples (A, B), where the items A are from this iterator and B are from the iterator given as argument. Like the zip method on ordinary iterators, if the two iterators are of unequal length, you only get the items they have in common. use rayo
docs.rs
일반 iter
https://doc.rust-lang.org/std/iter/trait.IntoIterator.htmlIntoIterator in std::iter - Rust
Which kind of iterator are we turning this into?
doc.rust-lang.org
반응형'코딩Coding > Rust❤️Optimization❤️' 카테고리의 다른 글
Rust 기본 PATH 및 Rust ❤️(rustup default nightly or stable)내맘대로 버젼 바꾸기❤️ (0) 2022.06.17 rust❤️] reduce vs fold의 차이(methods) (0) 2022.06.16 Rust❤️] map과 for_each의 차이점(for_each는 collect를 안 해도 된다 , 나같이 귀찮은거 싫어하는 사람에게 최고 구만ㅋ (0) 2022.06.16 cargo bench 사용법❤️-Benchmark testing your Rust Code | Let's Get Rusty (0) 2022.06.15 Cargo benchmark활용법 (0) 2022.06.14 Rust❤️]rayon 정의&Q&A 연습 및 관련 자료 (0) 2022.06.14 Rust❤️ VecDeque 벡터 맨 앞에 넣으려고 하면 VecDeque가 빠르다 (0) 2022.06.14 hyperfine 러스트 벤치마크 프로그램 benchmark (0) 2022.06.10