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 (tx, rx) = mpsc::channel();
- let 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 tx = tx.clone();
- let data = data.clone();
- let coef_arc = coef_arc.clone();
- let lower_bound_arc =lower_bound_arc.clone();
- thread::spawn(move || {
- // начало
- let mut data = data.lock().unwrap();
- let mut coef_arc = coef_arc.lock().unwrap();
- let mut lower_bound_arc = lower_bound_arc.lock().unwrap();
- let mut buf = polynom(&coef_arc, *lower_bound_arc);
- *lower_bound_arc+=0.01;
- 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!("text from {} , {} thread", i, count );
- tx.send(());
- // конец
- });
- }
- for i in 0..N{
- rx.recv();
- }
- println!("{:?}",data)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement