e.rs
· 3.8 KiB · Rust
Raw
use std::time::Instant;
use rand::prelude::*;
use std::f64::consts;
use console::Term;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
fn main() {
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
ctrlc::set_handler(move || {
r.store(false, Ordering::SeqCst);
println!();
}).expect("Error setting Ctrl-C handler");
let term = Term::stdout();
let show_progress = match std::env::var("SHOW_PROG").ok() {
Some(_) => true,
None => false
};
let max_loops: Option<u64> = match std::env::var("LOOPS") {
Ok(val) => Some(val.parse::<u64>().expect("not a valid integer")),
_ => None
};
match max_loops {
Some(max_loops) => {
println!("Running {} iterations. Press CTRL+C to exit at any time.", max_loops);
run_limited(term, running, show_progress, max_loops);
},
None => {
println!("No maximum loops specified, running forever. Press CTRL+C to exit at any time.");
run_unlimited(term, running, show_progress);
}
}
}
fn run_limited(term: Term, running: Arc<AtomicBool>, show_progress: bool, max_loops: u64) {
let now = Instant::now();
let mut total_count: u64 = 0;
let mut count: u64;
let mut sum: f64;
if show_progress {
println!("Iteration\tValue \t% Err\tCurrent %\tTime Elapsed");
}
for i in 0..max_loops {
count = 0;
sum = 0.0;
while sum <= 1.0 {
count += 1;
sum += rand::thread_rng().gen::<f64>();
}
total_count += count;
if show_progress && i % 1000 == 0 {
let average = total_count as f64 / i as f64;
let pererror = (average - consts::E).abs() / consts::E * 100.0;
let cur_percent = i as f64 / max_loops as f64 * 100.0;
term.write_line(&format!(" {:9.10}\t{:.10}\t{:5.2}%\t{:9.2}%\t{:?}", i, average, pererror, cur_percent, now.elapsed())).ok();
term.move_cursor_up(1).ok();
}
if !running.load(Ordering::SeqCst) {
break;
}
}
let average = total_count as f64 / max_loops as f64;
let pererror = (average - consts::E).abs() / consts::E * 100.0;
println!("---------------------------");
println!("Iterations: {}", max_loops);
println!("Final Average: {}", average);
println!("Final % Error: {:.4}%", pererror);
println!("Took: {:?}", now.elapsed());
}
fn run_unlimited(term: Term, running: Arc<AtomicBool>, show_progress: bool) {
let now = Instant::now();
let mut total_count: u64 = 0;
let mut count: u64;
let mut sum: f64;
let mut iteration: u64 = 0;
if show_progress {
println!("Iteration\tValue \t% Err\tTime Elapsed");
}
loop {
count = 0;
sum = 0.0;
while sum <= 1.0 {
count += 1;
sum += rand::thread_rng().gen::<f64>();
}
total_count += count;
if show_progress && iteration % 1000 == 0 {
let average = total_count as f64 / iteration as f64;
let pererror = (average - consts::E).abs() / consts::E * 100.0;
term.write_line(&format!(" {:9.10}\t{:.10}\t{:5.2}%\t{:?}", iteration, average, pererror, now.elapsed())).ok();
term.move_cursor_up(1).ok();
}
if !running.load(Ordering::SeqCst) {
break;
}
iteration += 1;
}
let average = total_count as f64 / iteration as f64;
let pererror = (average - consts::E).abs() / consts::E * 100.0;
println!("---------------------------");
println!("Iterations: {}", iteration);
println!("Final Average: {}", average);
println!("Final % Error: {:.4}%", pererror);
println!("Took: {:?}", now.elapsed());
}
| 1 | use std::time::Instant; |
| 2 | use rand::prelude::*; |
| 3 | use std::f64::consts; |
| 4 | use console::Term; |
| 5 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 6 | use std::sync::Arc; |
| 7 | |
| 8 | fn main() { |
| 9 | let running = Arc::new(AtomicBool::new(true)); |
| 10 | let r = running.clone(); |
| 11 | ctrlc::set_handler(move || { |
| 12 | r.store(false, Ordering::SeqCst); |
| 13 | println!(); |
| 14 | }).expect("Error setting Ctrl-C handler"); |
| 15 | |
| 16 | let term = Term::stdout(); |
| 17 | |
| 18 | let show_progress = match std::env::var("SHOW_PROG").ok() { |
| 19 | Some(_) => true, |
| 20 | None => false |
| 21 | }; |
| 22 | |
| 23 | let max_loops: Option<u64> = match std::env::var("LOOPS") { |
| 24 | Ok(val) => Some(val.parse::<u64>().expect("not a valid integer")), |
| 25 | _ => None |
| 26 | }; |
| 27 | |
| 28 | match max_loops { |
| 29 | Some(max_loops) => { |
| 30 | println!("Running {} iterations. Press CTRL+C to exit at any time.", max_loops); |
| 31 | run_limited(term, running, show_progress, max_loops); |
| 32 | }, |
| 33 | None => { |
| 34 | println!("No maximum loops specified, running forever. Press CTRL+C to exit at any time."); |
| 35 | run_unlimited(term, running, show_progress); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | fn run_limited(term: Term, running: Arc<AtomicBool>, show_progress: bool, max_loops: u64) { |
| 41 | let now = Instant::now(); |
| 42 | |
| 43 | let mut total_count: u64 = 0; |
| 44 | let mut count: u64; |
| 45 | let mut sum: f64; |
| 46 | |
| 47 | if show_progress { |
| 48 | println!("Iteration\tValue \t% Err\tCurrent %\tTime Elapsed"); |
| 49 | } |
| 50 | |
| 51 | for i in 0..max_loops { |
| 52 | count = 0; |
| 53 | sum = 0.0; |
| 54 | while sum <= 1.0 { |
| 55 | count += 1; |
| 56 | sum += rand::thread_rng().gen::<f64>(); |
| 57 | } |
| 58 | total_count += count; |
| 59 | if show_progress && i % 1000 == 0 { |
| 60 | let average = total_count as f64 / i as f64; |
| 61 | let pererror = (average - consts::E).abs() / consts::E * 100.0; |
| 62 | let cur_percent = i as f64 / max_loops as f64 * 100.0; |
| 63 | term.write_line(&format!(" {:9.10}\t{:.10}\t{:5.2}%\t{:9.2}%\t{:?}", i, average, pererror, cur_percent, now.elapsed())).ok(); |
| 64 | term.move_cursor_up(1).ok(); |
| 65 | } |
| 66 | |
| 67 | if !running.load(Ordering::SeqCst) { |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | let average = total_count as f64 / max_loops as f64; |
| 73 | let pererror = (average - consts::E).abs() / consts::E * 100.0; |
| 74 | println!("---------------------------"); |
| 75 | println!("Iterations: {}", max_loops); |
| 76 | println!("Final Average: {}", average); |
| 77 | println!("Final % Error: {:.4}%", pererror); |
| 78 | println!("Took: {:?}", now.elapsed()); |
| 79 | } |
| 80 | |
| 81 | fn run_unlimited(term: Term, running: Arc<AtomicBool>, show_progress: bool) { |
| 82 | let now = Instant::now(); |
| 83 | let mut total_count: u64 = 0; |
| 84 | let mut count: u64; |
| 85 | let mut sum: f64; |
| 86 | let mut iteration: u64 = 0; |
| 87 | |
| 88 | if show_progress { |
| 89 | println!("Iteration\tValue \t% Err\tTime Elapsed"); |
| 90 | } |
| 91 | |
| 92 | loop { |
| 93 | count = 0; |
| 94 | sum = 0.0; |
| 95 | while sum <= 1.0 { |
| 96 | count += 1; |
| 97 | sum += rand::thread_rng().gen::<f64>(); |
| 98 | } |
| 99 | total_count += count; |
| 100 | if show_progress && iteration % 1000 == 0 { |
| 101 | let average = total_count as f64 / iteration as f64; |
| 102 | let pererror = (average - consts::E).abs() / consts::E * 100.0; |
| 103 | term.write_line(&format!(" {:9.10}\t{:.10}\t{:5.2}%\t{:?}", iteration, average, pererror, now.elapsed())).ok(); |
| 104 | term.move_cursor_up(1).ok(); |
| 105 | } |
| 106 | |
| 107 | if !running.load(Ordering::SeqCst) { |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | iteration += 1; |
| 112 | } |
| 113 | |
| 114 | let average = total_count as f64 / iteration as f64; |
| 115 | let pererror = (average - consts::E).abs() / consts::E * 100.0; |
| 116 | println!("---------------------------"); |
| 117 | println!("Iterations: {}", iteration); |
| 118 | println!("Final Average: {}", average); |
| 119 | println!("Final % Error: {:.4}%", pererror); |
| 120 | println!("Took: {:?}", now.elapsed()); |
| 121 | } |