Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::io;
- use std::vec::Vec;
- use std::env;
- use std::time::{ SystemTime,Duration, UNIX_EPOCH};
- use std::thread::sleep;
- use std::fmt;
- use std::thread;
- use std::sync::{Arc, Mutex};
- use std::sync::mpsc;
- use std::thread::JoinHandle;
- fn polynom(coef: &Vec<f64>, point: f64) -> f64 {
- let mut buf = 0.0;
- for i in 0..=coef.len()-1{
- buf += coef[i] * (f64::powi(point, (coef.len()-i-1) as i32));
- }
- buf
- }
- /*fn point_counter(coef: &Vec<f64>, lower_bound: f64, upper_bound:f64) -> i128{
- let mut count = 0;
- let step = (upper_bound-lower_bound)/1000000000.0;
- let mut buf = 0.0;
- let mut point_now = lower_bound;
- let sixty_seconds = Duration::new(60, 0); // переменная, равная 60 секунд
- let start = SystemTime::now();
- let start_S = start.duration_since(UNIX_EPOCH).expect("Time went backwards");
- let mut now = SystemTime::now();
- let mut now_S = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
- while (now_S-start_S <=sixty_seconds){
- buf = polynom(&coef, point_now);
- point_now+=step;
- count+=1;
- now = SystemTime::now();
- now_S = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
- }
- //println!("{:?}", now_S-start_S);
- count
- }
- fn point_timer(coef: &Vec<f64>, lower_bound: f64, upper_bound:f64, N: i128){
- let step = (upper_bound-lower_bound)/1000000000.0;
- let mut buf = 0.0;
- let mut point_now = lower_bound;
- let now = SystemTime::now();
- for i in 0..N-1{
- buf = polynom(&coef, point_now);
- point_now+=step;
- }
- let finish = SystemTime::now();
- let since_the_epoch_finish = finish.duration_since(UNIX_EPOCH).expect("Time went backwards");
- let since_the_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
- let delta_time = since_the_epoch_finish - since_the_epoch;
- println!("{:?}", delta_time);
- }
- fn ten_times_runner(coef: &Vec<f64>, lower_bound: f64, upper_bound:f64, N: i128){
- for i in 0..=11{
- println!("{} раз:",i+1);
- point_timer(coef, lower_bound, upper_bound, N);
- }
- }*/
- fn main() {
- // считываем аргументы командной строки
- let args: Vec<String> = env::args().collect();
- let mut coef: Vec<f64> = vec![0.0;args.len()-3];
- //println!("args size= {}", args.len());
- let x = args[1].parse::<f64>().unwrap();
- let y = args[2].parse::<f64>().unwrap();
- let mut lower_bound = f64::min(x,y);
- let upper_bound = f64::max(x,y);
- //let step : f64 = (upper_bound-lower_bound)/((N-1) as f64);
- //преобразуем аргументы КС из string в double
- for i in 3..=args.len()-1{
- coef[i-3] = args[i].parse::<f64>().unwrap();
- //println!("{} ",coef[i-3]);
- }
- //let c : Vec<f64> = coef.clone();
- ///////////////////
- //let (tx, rx) = mpsc::channel();
- let mut N = 4;
- /*let data = Arc::new(Mutex::new(vec![0 as u32; N as usize]));
- let coef_arc = Arc::new(Mutex::new(coef));
- let lower_bound_arc = Arc::new(Mutex::new(lower_bound));*/
- /*for i in 0..N {
- let (txc, datac, coef_arcc, lower_bound_arcc) =
- (tx.clone(), data.clone(), coef_arc.clone(),lower_bound_arc.clone());
- thread::spawn(move || {
- // начало
- println!("{} thread start", i );
- let mut data = datac.lock().unwrap();
- let coef_arc = coef_arcc.lock().unwrap();
- let mut lower_bound_arc = lower_bound_arcc.lock().unwrap();
- let mut buf = 0.0;
- let sixty_seconds = Duration::new(10, 0);
- let start = SystemTime::now();
- let start_S = start.duration_since(UNIX_EPOCH).expect("Time went backwards");
- let mut now = SystemTime::now();
- let mut now_S = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
- let mut count =0;
- while (now_S-start_S <=sixty_seconds){
- buf = polynom(&coef_arc, *lower_bound_arc);
- count+=1;
- now = SystemTime::now();
- now_S = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
- }
- data[i] = count;
- println!("{} thread finish {} count", i, count );
- txc.send(());
- // конец
- });
- }
- for i in 0..N{
- rx.recv();
- }*/
- N = 6;
- let mut children_threads = vec![];
- let mut c = vec![];
- for i in 0..N{
- c.push(vec![]);
- c[i] = coef.to_owned();
- }
- //let coef_arc = Arc::new(Mutex::new(coef));
- let mut cc = vec![];
- for i in 0..N{
- cc.push(Arc::new(Mutex::new(c[i].to_owned())));
- }
- for i in 0..N{
- let coef_arc_ = cc[i].clone();
- children_threads.push(thread::spawn(move || {
- println!("{} thread start",i);
- //let coef_arc_ = cc[i].lock().unwrap();
- let coef_arc_ = coef_arc_.lock().unwrap();
- let mut buf = 0.0;
- for j in 0..10000000{
- buf = polynom(&coef_arc_,10.0);
- //buf = coef_arc_[0];
- if (j%1000000==0){ println!("{} thread do {} points",i,j);}
- }
- println!("{} thread finish!",i);
- }));
- }
- for child in children_threads {
- let _ = child.join();
- }
- println!();
- println!();
- /*let coef2 = coef.clone();
- let coef_ = Arc::new(Mutex::new(coef));
- let mut count1 = Arc::new(Mutex::new(0));
- let thread1 = thread::spawn(move || {
- // начало
- println!("1 thread start");
- let mut count1 = count1.lock().unwrap();
- let coef_ = coef_.lock().unwrap();
- let mut buf=0.0;
- for i in 0..10000000{
- buf=polynom(&coef_,10.0);
- if (i%1000000==0){
- println!("1 thread do {} points", i );
- }
- }
- println!("1 thread finish!" );
- });
- let coef2_ = Arc::new(Mutex::new(coef2));
- let mut count2 = Arc::new(Mutex::new(0));
- let thread2 = thread::spawn(move || {
- // начало
- println!("2 thread start");
- let mut count2 = count2.lock().unwrap();
- let coef2_ = coef2_.lock().unwrap();
- let mut buf=0.0;
- for i in 0..10000000{
- buf=polynom(&coef2_,10.0);
- if (i%1000000==0){
- println!("2 thread do {} points", i );
- }
- }
- println!("2 thread finish!");
- });
- thread1.join();
- thread2.join();*/
- //println!("{} , {} ",*count1, *count2)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement