Advertisement
Guest User

Untitled

a guest
Nov 1st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.21 KB | None | 0 0
  1. use std::io;
  2. use std::vec::Vec;
  3. use std::env;
  4. use std::time::{ SystemTime,Duration, UNIX_EPOCH};
  5. use std::thread::sleep;
  6.  
  7. use std::fmt;
  8. use std::thread;
  9. use std::sync::{Arc, Mutex};
  10. use std::sync::mpsc;
  11. use std::thread::JoinHandle;
  12.  
  13.  
  14. fn polynom(coef: &Vec<f64>, point: f64) -> f64 {
  15.     let mut buf = 0.0;
  16.     for i in 0..=coef.len()-1{
  17.         buf += coef[i] * (f64::powi(point, (coef.len()-i-1) as i32));
  18.     }
  19.     buf
  20. }
  21.  
  22. fn point_counter(coef: &Vec<f64>, lower_bound: f64, upper_bound:f64) -> i128{
  23.     let mut count = 0;
  24.     let step = (upper_bound-lower_bound)/1000000000.0;
  25.     let mut buf = 0.0;
  26.     let mut point_now = lower_bound;
  27.    
  28.     let sixty_seconds = Duration::new(60, 0); // переменная, равная 60 секунд
  29.  
  30.     let start = SystemTime::now();
  31.     let start_S = start.duration_since(UNIX_EPOCH).expect("Time went backwards");
  32.    
  33.     let mut now = SystemTime::now();
  34.     let mut now_S = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
  35.    
  36.     while (now_S-start_S <=sixty_seconds){
  37.         buf = polynom(&coef, point_now);
  38.         point_now+=step;
  39.         count+=1;
  40.         now = SystemTime::now();
  41.         now_S = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
  42.     }
  43.     //println!("{:?}", now_S-start_S);
  44.     count
  45. }
  46.  
  47. fn point_timer(coef: &Vec<f64>, lower_bound: f64, upper_bound:f64, N: i128){
  48.     let step = (upper_bound-lower_bound)/1000000000.0;
  49.     let mut buf = 0.0;
  50.     let mut point_now = lower_bound;
  51.    
  52.     let now = SystemTime::now();
  53.     for i in 0..N-1{
  54.         buf = polynom(&coef, point_now);
  55.         point_now+=step;
  56.     }
  57.  
  58.     let finish = SystemTime::now();
  59.     let since_the_epoch_finish = finish.duration_since(UNIX_EPOCH).expect("Time went backwards");
  60.     let since_the_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
  61.     let delta_time = since_the_epoch_finish - since_the_epoch;
  62.     println!("{:?}", delta_time);
  63. }
  64.  
  65. fn ten_times_runner(coef: &Vec<f64>, lower_bound: f64, upper_bound:f64, N: i128){
  66.     for i in 0..=11{
  67.         println!("{} раз:",i+1);
  68.         point_timer(coef, lower_bound, upper_bound, N);
  69.     }
  70. }
  71. fn main() {
  72.  
  73.     // считываем аргументы командной строки
  74.     let args: Vec<String> = env::args().collect();
  75.     let mut coef: Vec<f64> = vec![0.0;args.len()-3];
  76.     //println!("args size= {}", args.len());
  77.  
  78.     let x = args[1].parse::<f64>().unwrap();
  79.     let y = args[2].parse::<f64>().unwrap();
  80.     let mut lower_bound = f64::min(x,y);
  81.     let upper_bound = f64::max(x,y);
  82.     //let step : f64 = (upper_bound-lower_bound)/((N-1) as f64);
  83.  
  84.     //преобразуем аргументы КС из string в double
  85.     for i in 3..=args.len()-1{
  86.         coef[i-3] = args[i].parse::<f64>().unwrap();
  87.         //println!("{} ",coef[i-3]);
  88.     }
  89.     ///////////////////
  90.    
  91.     let (tx, rx) = mpsc::channel();
  92.     let N = 4;
  93.     let data = Arc::new(Mutex::new(vec![0 as u32; N as usize]));
  94.     let coef_arc = Arc::new(Mutex::new(coef));
  95.     let lower_bound_arc = Arc::new(Mutex::new(lower_bound));
  96.         for i in 0..N {
  97.                 let tx = tx.clone();
  98.                 let data = data.clone();
  99.                 let coef_arc = coef_arc.clone();
  100.                 let lower_bound_arc =lower_bound_arc.clone();
  101.                 thread::spawn(move || {
  102.                     // начало
  103.                     let mut data = data.lock().unwrap();
  104.                     let mut coef_arc = coef_arc.lock().unwrap();
  105.                     let mut lower_bound_arc = lower_bound_arc.lock().unwrap();
  106.                     let mut buf = polynom(&coef_arc, *lower_bound_arc);
  107.                     *lower_bound_arc+=0.01;
  108.  
  109.                     let sixty_seconds = Duration::new(10, 0);
  110.                     let start = SystemTime::now();
  111.                     let start_S = start.duration_since(UNIX_EPOCH).expect("Time went backwards");
  112.                     let mut now = SystemTime::now();
  113.                     let mut now_S = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
  114.                     let mut count =0;
  115.  
  116.                     while (now_S-start_S <=sixty_seconds){
  117.                         buf = polynom(&coef_arc, *lower_bound_arc);
  118.                         count+=1;
  119.                         now = SystemTime::now();
  120.                         now_S = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
  121.                     }
  122.                     data[i] = count;
  123.  
  124.                     println!("text from {} ,  {} thread", i, count );
  125.                     tx.send(());
  126.                     // конец
  127.             });
  128.     }
  129.     for i in 0..N{
  130.         rx.recv();
  131.     }
  132.    
  133.     println!("{:?}",data)
  134.  
  135.  
  136.  
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement