Advertisement
Guest User

Untitled

a guest
Nov 4th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 6.14 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.     //let c : Vec<f64> = coef.clone();
  90.     ///////////////////
  91.    
  92.     //let (tx, rx) = mpsc::channel();
  93.     let mut N = 4;
  94.     /*let data = Arc::new(Mutex::new(vec![0 as u32; N as usize]));
  95.     let coef_arc = Arc::new(Mutex::new(coef));
  96.     let lower_bound_arc = Arc::new(Mutex::new(lower_bound));*/
  97.         /*for i in 0..N {
  98.                 let (txc, datac, coef_arcc, lower_bound_arcc) =
  99.                  (tx.clone(),  data.clone(), coef_arc.clone(),lower_bound_arc.clone());
  100.                 thread::spawn(move || {
  101.                     // начало
  102.                     println!("{} thread start", i );
  103.                     let mut data = datac.lock().unwrap();
  104.                     let coef_arc = coef_arcc.lock().unwrap();
  105.                     let mut lower_bound_arc = lower_bound_arcc.lock().unwrap();
  106.                     let mut buf = 0.0;
  107.  
  108.                     let sixty_seconds = Duration::new(10, 0);
  109.                     let start = SystemTime::now();
  110.                     let start_S = start.duration_since(UNIX_EPOCH).expect("Time went backwards");
  111.                     let mut now = SystemTime::now();
  112.                     let mut now_S = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
  113.                     let mut count =0;
  114.  
  115.                     while (now_S-start_S <=sixty_seconds){
  116.                
  117.                         buf = polynom(&coef_arc, *lower_bound_arc);
  118.  
  119.                         count+=1;
  120.                         now = SystemTime::now();
  121.                         now_S = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
  122.                     }
  123.                     data[i] = count;
  124.  
  125.                     println!("{} thread finish {} count", i, count );
  126.                     txc.send(());
  127.                     // конец
  128.             });
  129.            
  130.  
  131.     }
  132.     for i in 0..N{
  133.         rx.recv();
  134.     }*/
  135.     N = 6;
  136.     let mut children_threads = vec![];
  137.     let mut c = vec![];
  138.     for i in 0..N{
  139.         c.push(vec![]);
  140.         c[i] = coef.to_owned();
  141.     }
  142.     //let coef_arc = Arc::new(Mutex::new(coef));
  143.     let mut cc = vec![];
  144.     for i in 0..N{
  145.         cc.push(Arc::new(Mutex::new(c[i].to_owned())));
  146.     }
  147.     for i in 0..N{
  148.        
  149.         let coef_arc_ = cc[i].clone();
  150.         children_threads.push(thread::spawn(move || {
  151.                 println!("{} thread start",i);
  152.                 //let coef_arc_ = cc[i].lock().unwrap();
  153.                 let coef_arc_ = coef_arc_.lock().unwrap();
  154.                 let mut buf = 0.0;
  155.                 for j in 0..10000000{
  156.                     buf = polynom(&coef_arc_,10.0);
  157.                     //buf = coef_arc_[0];
  158.                     if (j%1000000==0){ println!("{} thread do {} points",i,j);}
  159.                 }
  160.                 println!("{} thread finish!",i);
  161.                
  162.             }));
  163.     }
  164.     for child in children_threads {
  165.         let _ = child.join();
  166.     }
  167. println!();
  168. println!();
  169.  
  170.    
  171.  
  172.     /*let coef2 = coef.clone();
  173.     let coef_ = Arc::new(Mutex::new(coef));
  174.     let mut count1 = Arc::new(Mutex::new(0));
  175.     let thread1 = thread::spawn(move || {
  176.                     // начало
  177.                     println!("1 thread start");
  178.                     let mut count1 = count1.lock().unwrap();
  179.                     let coef_ = coef_.lock().unwrap();
  180.                     let mut buf=0.0;
  181.                     for i in 0..10000000{
  182.                         buf=polynom(&coef_,10.0);
  183.                         if (i%1000000==0){
  184.                             println!("1 thread do {} points", i );
  185.                         }
  186.                     }
  187.                     println!("1 thread finish!" );
  188.     });
  189.    
  190.     let coef2_ = Arc::new(Mutex::new(coef2));
  191.     let mut count2 = Arc::new(Mutex::new(0));
  192.     let thread2 = thread::spawn(move || {
  193.                     // начало
  194.                     println!("2 thread start");
  195.                     let mut count2 = count2.lock().unwrap();
  196.                     let coef2_ = coef2_.lock().unwrap();
  197.                     let mut buf=0.0;
  198.                     for i in 0..10000000{
  199.                         buf=polynom(&coef2_,10.0);
  200.                         if (i%1000000==0){
  201.                             println!("2 thread do {} points", i );
  202.                         }
  203.                     }
  204.                     println!("2 thread finish!");
  205.     });
  206.     thread1.join();
  207.     thread2.join();*/
  208.  
  209.  
  210.     //println!("{} , {} ",*count1, *count2)
  211.  
  212.  
  213.  
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement