-
rust❤️) 기차 모양 프린트 & multithread연습 + C++과 비교해서 연습코딩Coding/한글Rust강의★내가★공부하려고만듬 2022. 6. 10. 09:44728x90
C++ con
http://www.digipine.com/index.php?mid=clan&document_srl=584
러스트
Concurrency 시작
참조
Rust_concurrency❤️최적화
- Rayon을 써야 할 때와 tokio를 써야 할 상황 알아 보기 &❤️futures concurrency
- https://economiceco.tistory.com/m/13736
fn main() { let train_char1: char = '🚂'; let mut space_no:usize = 15; while space_no != 0 { space_no -= 1; println!("{train_char1:>space_no$}"); } }
결과Compiling playground v0.0.1 (/playground) Finished dev [unoptimized + debuginfo] target(s) in 0.71s Running `target/debug/playground` Standard Output 🚂 🚂 🚂 🚂 🚂 🚂 🚂 🚂 🚂 🚂 🚂 🚂 🚂 🚂 🚂
이거 추가하고
0.5초 쉬게 하고 하면 될 듯 싶음
linix macOsstd::process::Command::new("clear").status().unwrap();
Windows는std::process::Command::new("cls").status().unwrap();
운영체제 별로 실행 하게 만드는 방법
윈도일때 리눅스일 때
실행하는 코드를 control 할 수 있음
https://youtu.be/uvJGGBhBfgI#[cfg(target_os = "macos"] 이러면 되려나??
Rust❤️) #cfg(target_os = "linux")운영체제 별로 실행하는 코드 control하기 ❤️
- https://economiceco.tistory.com/m/14038
https://doc.rust-lang.org/rust-by-example/attribute/cfg.html
화면 깨끗이 만들기
이거 좋네
https://stackoverflow.com/questions/34837011/how-to-clear-the-terminal-screen-in-rust-after-a-new-line-is-printed
multithread
https://youtu.be/_AQDYmZRmRc
이 영상 참조해 만듦
이 코드
출처 원본
https://rust-lang-nursery.github.io/rust-cookbook/concurrency/parallel.html
위 Cookbook연습use rayon::prelude::*; fn main() { let mut vec = vec![2, 4, 6, 8]; let letters = "!vec vs vec vec![2, 4, 6, 8];".to_string(); let width:usize = 6; println!("{letters:>width$}"); let x = !vec.par_iter().any(|n| (*n % 2) != 0); println!("vec.par_iter().any() = {x}"); let x_1 = !vec.par_iter().any(|n| (*n % 2) == 0); println!(" !vec.par_iter().any = {x_1}"); println!("\nvec.par_iter().all"); let x_2 = vec.par_iter().all(|n| (*n % 2) == 0); println!("vec.par_iter().all = {} ", x_2); println!("\nvec vs vec vs any vs all"); let x_3 = !vec.par_iter().any(|n| *n > 8 ); println!("!vec.par_iter().any = {} ", x_3); let x_4 = vec.par_iter().all(|n| *n <= 8 ); println!("vec.par_iter().all(|n| *n <= 8 : {}", x_4); vec.push(9); println!("\npush(9). vec![2, 4, 6, 8, 9]; "); let y_1 = vec.par_iter().any(|n| (*n % 2) != 0); println!("vec.par_iter().any(|n| (*n % 2) != 0 : {}", y_1); let y_2 = !vec.par_iter().all(|n| (*n % 2) == 0); println!("vec.par_iter().all(|n| (*n % 2) != 0 : {}", y_2); let y_3 = vec.par_iter().any(|n| *n > 8 ); println!("vec.par_iter().any(|n| *n > 8 ) : {}", y_3); let y_4 = !vec.par_iter().all(|n| *n <= 8 ); println!("!vec.par_iter().all(|n| *n <= 8 ) : {}", y_4); }
결과Compiling playground v0.0.1 (/playground) Finished dev [unoptimized + debuginfo] target(s) in 1.38s Running `target/debug/playground` Standard Output !vec vs vec vec![2, 4, 6, 8]; vec.par_iter().any() = true !vec.par_iter().any = false vec.par_iter().all vec.par_iter().all = true vec vs vec vs any vs all !vec.par_iter().any = true vec.par_iter().all(|n| *n <= 8 : true push(9). vec![2, 4, 6, 8, 9]; vec.par_iter().any(|n| (*n % 2) != 0 : true vec.par_iter().all(|n| (*n % 2) != 0 : true vec.par_iter().any(|n| *n > 8 ) : true !vec.par_iter().all(|n| *n <= 8 ) : true
rayon 연습
use rayon::prelude::*; fn increment_all(input: &mut [i32]) { input.par_iter_mut() .for_each(|p| *p += 1); println!("thread : {input:?}"); } fn main() { increment_all(&mut [10]); }
결과Compiling playground v0.0.1 (/playground) Finished dev [unoptimized + debuginfo] target(s) in 1.13s Running `target/debug/playground` Standard Output thread : [11]
true1 / false 0 (truth table)진리표
- https://economiceco.tistory.com/m/14060
러스트 true 1 . false 가 0
껴짐 / 꺼짐인듯
On / Off
https://doc.rust-lang.org/std/primitive.bool.html
https://docs.rs/rayon/1.0.3/rayon/iter/index.html
use rayon::prelude::*; fn increment_all(input: &mut [i32]) { input.par_iter_mut() .for_each(|p| *p += 1); println!("{:?}", input); } fn main() { println!("thread1 : {:?}", increment_all(&mut [10])); }
[11] thread1 : ()
실패 코드use rayon::prelude::*; fn increment_all(input: &mut [i32]) { input.par_iter_mut() .for_each(|p| *p += 1); match input { [11] => 11, _ => 0, }; } fn main() { println!("thread1 : {:?}, ", increment_all(&mut [10])); }
결과Compiling playground v0.0.1 (/playground) Finished dev [unoptimized + debuginfo] target(s) in 0.92s Running `target/debug/playground` Standard Output thread1 : (),
https://doc.rust-lang.org/std/ops/struct.Range.html
https://doc.rust-lang.org/std/time/struct.Instant.htmluse rayon::prelude::*; use std::time::{Duration, Instant}; fn increment_all(input: &mut [i32]) { input.par_iter_mut().for_each(|p| *p += 1); for input in (std::ops::Range { start: 10, end: 100000 }) { println!("{:?}", input); } } fn main() { let now_1 = Instant::now(); let now_2 = Instant::now(); print!("thread1 : "); print!("{:?}", increment_all(&mut [10])); println!("elapse : {}", now_1.elapsed().as_millis()); print!("thread2 : "); print!("{:?}", increment_all(&mut [10])); println!("elapse : {}", now_2.elapsed().as_millis()); }
결과
...
...
use rayon::prelude::*; use std::time::{Duration, Instant}; fn increment_all(input: &mut [i32]) { input.par_iter_mut().for_each(|p| *p += 1); for input in (std::ops::Range { start: 10, end: 10000 }) { println!("{:?}", input); } } fn main() { let now_1 = Instant::now(); let now_2 = Instant::now(); print!("thread1 : "); print!("{:?}", increment_all(&mut [10])); let elapsed1= now_1.elapsed().as_millis(); print!("thread2 : "); print!("{:?}", increment_all(&mut [10])); let elapsed2 = now_2.elapsed().as_millis(); println!("1nd thread elapse : {elapsed1} millsec"); println!("2nd thread elapse : {elapsed2} millsec"); }
결과9995 9996 9997 9998 9999 ()1nd thread elapse : 27 millsec 2nd thread elapse : 62 millsec
decrement(--)하는 법use rayon::prelude::*; use std::time::{Duration, Instant}; fn increment_all(input: &mut [i32]) { input.par_iter_mut().for_each(|p| *p += 1); for i in (0..1000).rev() { println!("{:?}", i); } } fn main() { let now_1 = Instant::now(); let now_2 = Instant::now(); print!("thread1 : "); print!("{:?}", increment_all(&mut [10])); let elapsed1= now_1.elapsed().as_millis(); print!("thread2 : "); print!("{:?}", increment_all(&mut [10])); let elapsed2 = now_2.elapsed().as_millis(); println!("1nd thread elapse : {elapsed1} millsec"); println!("2nd thread elapse : {elapsed2} millsec"); }
https://stackoverflow.com/questions/25170091/how-to-make-a-reverse-ordered-for-loop
Rust❤️
increment(++)
decrement(--)하는 법
- https://economiceco.tistory.com/m/14045https://economiceco.tistory.com/14064
C++로 Concurrency 구현
https://cmj092222.tistory.com/542
반응형'코딩Coding > 한글Rust강의★내가★공부하려고만듬' 카테고리의 다른 글
Rust vs Cpp ❤️threads 한글 강의 (0) 2022.06.11 Rust Futures❤️에 대해 공부하기_Concurrency기초 (0) 2022.06.11 Rust❤️Bevy_Game기초 동영상 만들 예정❤️ (0) 2022.06.11 Rust❤️Bubble sort와 Timsort & PDQSort 성능 비교 예정 (0) 2022.06.10 Rust❤️byExample한글 버젼 full tutorial❤️로 동영상 만들 예정 (0) 2022.06.08 Rust to Redis❤️with❤️Async/ Await - Jeremy Chone (0) 2022.06.07 Rust앱만들기-Todo App최고❤️Rust Web App (0) 2022.06.06 Rust egui ❤️동영상 만들 예정 (0) 2022.06.02