-
rust❤️) 기차 모양 프린트 & multithread연습 + C++과 비교해서 연습코딩Coding/한글Rust강의★내가★공부하려고만듬 2022. 6. 10. 09:44728x90
C++ con
http://www.digipine.com/index.php?mid=clan&document_srl=584
C/C++ Language - [Linux] Pthread 사용법, Thread 동기화 총정리
Pthread API Reference 1.1. pthread_create int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); 쓰레드 생성을 위해서 사용한다. 첫번째 아규먼트인 thread 는 쓰레드가 성공적
www.digipine.com
러스트
Concurrency 시작
참조
Rust_concurrency❤️최적화
- Rayon을 써야 할 때와 tokio를 써야 할 상황 알아 보기 &❤️futures concurrency
- https://economiceco.tistory.com/m/13736Rust_concurrency❤️최적화 - Rayon을 써야 할 때와 tokio를 써야 할 상황 알아 보기 &❤️futures concurrency
Rayon 기초 https://youtu.be/gof_OEv71Aw rayon https://github.com/rayon-rs/rayon GitHub - rayon-rs/rayon: Rayon: A data parallelism library for Rust Rayon: A data parallelism library for Rust. Contr..
economiceco.tistory.com
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/14038Rust❤️) #cfg(target_os = "linux")운영체제 별로 실행하는 코드 control하기 ❤️
운영체제 별로 실행 하게 만드는 방법 윈도일때 리눅스일 때 실행하는 코드를 control 할 수 있음 https://youtu.be/uvJGGBhBfgI
economiceco.tistory.com
https://doc.rust-lang.org/rust-by-example/attribute/cfg.htmlcfg - Rust By Example
Configuration conditional checks are possible through two different operators: the cfg attribute: #[cfg(...)] in attribute position the cfg! macro: cfg!(...) in boolean expressions While the former enables conditional compilation, the latter conditionally
doc.rust-lang.org
화면 깨끗이 만들기
이거 좋네
https://stackoverflow.com/questions/34837011/how-to-clear-the-terminal-screen-in-rust-after-a-new-line-is-printedHow to clear the terminal screen in Rust after a new line is printed?
I have printed some text using println! and now I need to clear the terminal and write the new text instead of the old. How can I clear all the current text from terminal? I have tried this code, b...
stackoverflow.com
multithread
https://youtu.be/_AQDYmZRmRc
이 영상 참조해 만듦
이 코드
출처 원본
https://rust-lang-nursery.github.io/rust-cookbook/concurrency/parallel.htmlData Parallelism - Rust Cookbook
The example uses the rayon crate, which is a data parallelism library for Rust. rayon provides the par_iter_mut method for any parallel iterable data type. This is an iterator-like chain that potentially executes in parallel. use rayon::prelude::*; fn main
rust-lang-nursery.github.io
위 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/14060true1 / false 0 (truth table)진리표
http://www.tcpschool.com/codingmath/boolean 코딩교육 티씨피스쿨 4차산업혁명, 코딩교육, 소프트웨어교육, 코딩기초, SW코딩, 기초코딩부터 자바 파이썬 등 tcpschool.com
economiceco.tistory.com
러스트 true 1 . false 가 0
껴짐 / 꺼짐인듯
On / Off
https://doc.rust-lang.org/std/primitive.bool.htmlbool - Rust
The boolean type. The bool represents a value, which could only be either true or false. If you cast a bool into an integer, true will be 1 and false will be 0. bool implements various traits, such as BitAnd, BitOr, Not, etc., which allow us to perform boo
doc.rust-lang.org
https://docs.rs/rayon/1.0.3/rayon/iter/index.htmlrayon::iter - Rust
Traits for writing parallel programs using an iterator-style interface You will rarely need to interact with this module directly unless you have need to name one of the iterator types. Parallel iterators make it easy to write iterator-like chains that exe
docs.rs
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.htmlRange in std::ops - Rust
pub struct Range { pub start: Idx, pub end: Idx, } Expand descriptionA (half-open) range bounded inclusively below and exclusively above (start..end). The range start..end contains all values with start <= x < end. It is empty if start >= end. The start..e
doc.rust-lang.org
https://doc.rust-lang.org/std/time/struct.Instant.htmlInstant in std::time - Rust
Returns Some(t) where t is the time self - duration if t can be represented as Instant (which means it’s inside the bounds of the underlying data structure), None otherwise.
doc.rust-lang.org
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: 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-loopHow to make a reverse ordered for loop?
Editor's note: This question was asked before Rust 1.0 was released and the .. "range" operator was introduced. The question's code no longer represents the current style, but some answers below uses
stackoverflow.com
Rust❤️
increment(++)
decrement(--)하는 법
- https://economiceco.tistory.com/m/14045Rust❤️ increment(++) decrement(--)하는 법
https://www.reddit.com/r/rust/comments/jf66eu/why_are_there_no_increment_and_decrement/ Why are there no increment (++) and decrement (--) operators in Rust? I've just started learning Rust, and it..
economiceco.tistory.com
https://economiceco.tistory.com/14064
Rust Futures❤️에 대해 공부하기_Concurrency기초
https://cfsamson.github.io/books-futures-explained/0_background_information.html Background information - Futures Explained in 200 Lines of Rust Before we go into the details about Futures in Rust,..
economiceco.tistory.com
C++로 Concurrency 구현
https://cmj092222.tistory.com/542Parallelism
※ Exploiting parallel execution 지금까지는 스레드를 사용하여 I/O 지연을 처리했다. 예를 들어, 클라이언트당 하나의 스레드를 사용하여 다른 스레드를 지연시키는 것을 방지한다. Multi-core/Hyperthreaded
cmj092222.tistory.com
반응형'코딩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