jackz revised this gist 10 months ago. Go to revision
No changes
Jackz revised this gist 4 years ago. Go to revision
1 file changed, 0 insertions, 0 deletions
e.rs renamed to ecalculator.rs
File renamed without changes
Jackz revised this gist 4 years ago. Go to revision
1 file changed, 34 insertions, 18 deletions
e.rs
| @@ -6,6 +6,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; | |||
| 6 | 6 | use std::sync::Arc; | |
| 7 | 7 | ||
| 8 | 8 | fn main() { | |
| 9 | + | //Handler to terminate on CTRL+C | |
| 9 | 10 | let running = Arc::new(AtomicBool::new(true)); | |
| 10 | 11 | let r = running.clone(); | |
| 11 | 12 | ctrlc::set_handler(move || { | |
| @@ -15,48 +16,57 @@ fn main() { | |||
| 15 | 16 | ||
| 16 | 17 | let term = Term::stdout(); | |
| 17 | 18 | ||
| 18 | - | let show_progress = match std::env::var("SHOW_PROG").ok() { | |
| 19 | - | Some(_) => true, | |
| 20 | - | None => false | |
| 19 | + | //Parses env 'SHOW_PROGRESS', value must be an int and sets how frequent to log progress | |
| 20 | + | //If has a value, should log progress. | |
| 21 | + | let log_interval: Option<u64> = match std::env::var("SHOW_PROGRESS").ok() { | |
| 22 | + | Some(value) => Some(value.parse::<u64>().expect("not a valid integer")), | |
| 23 | + | None => None | |
| 21 | 24 | }; | |
| 22 | 25 | ||
| 26 | + | //Gets the maximum loops to run or none for infinity | |
| 23 | 27 | let max_loops: Option<u64> = match std::env::var("LOOPS") { | |
| 24 | 28 | Ok(val) => Some(val.parse::<u64>().expect("not a valid integer")), | |
| 25 | 29 | _ => None | |
| 26 | 30 | }; | |
| 27 | - | ||
| 31 | + | ||
| 28 | 32 | match max_loops { | |
| 29 | 33 | Some(max_loops) => { | |
| 30 | 34 | println!("Running {} iterations. Press CTRL+C to exit at any time.", max_loops); | |
| 31 | - | run_limited(term, running, show_progress, max_loops); | |
| 35 | + | run_limited(term, running, log_interval, max_loops); | |
| 32 | 36 | }, | |
| 33 | 37 | None => { | |
| 34 | 38 | println!("No maximum loops specified, running forever. Press CTRL+C to exit at any time."); | |
| 35 | - | run_unlimited(term, running, show_progress); | |
| 39 | + | run_unlimited(term, running, log_interval); | |
| 36 | 40 | } | |
| 37 | 41 | } | |
| 38 | 42 | } | |
| 39 | 43 | ||
| 40 | - | fn run_limited(term: Term, running: Arc<AtomicBool>, show_progress: bool, max_loops: u64) { | |
| 44 | + | //Runs upto X times | |
| 45 | + | fn run_limited(term: Term, running: Arc<AtomicBool>, log_interval: Option<u64>, max_loops: u64) { | |
| 41 | 46 | let now = Instant::now(); | |
| 42 | 47 | ||
| 43 | 48 | let mut total_count: u64 = 0; | |
| 44 | 49 | let mut count: u64; | |
| 45 | 50 | let mut sum: f64; | |
| 46 | 51 | ||
| 47 | - | if show_progress { | |
| 52 | + | //Print header only if showing progress | |
| 53 | + | if log_interval.is_some() { | |
| 48 | 54 | println!("Iteration\tValue \t% Err\tCurrent %\tTime Elapsed"); | |
| 49 | 55 | } | |
| 50 | 56 | ||
| 51 | 57 | for i in 0..max_loops { | |
| 52 | 58 | count = 0; | |
| 53 | 59 | sum = 0.0; | |
| 60 | + | //Run until sum hits 1.0 | |
| 54 | 61 | while sum <= 1.0 { | |
| 55 | 62 | count += 1; | |
| 56 | 63 | sum += rand::thread_rng().gen::<f64>(); | |
| 57 | 64 | } | |
| 65 | + | ||
| 58 | 66 | total_count += count; | |
| 59 | - | if show_progress && i % 1000 == 0 { | |
| 67 | + | ||
| 68 | + | //Prints progress: | |
| 69 | + | if log_interval.is_some() && i % log_interval.unwrap() == 0 { | |
| 60 | 70 | let average = total_count as f64 / i as f64; | |
| 61 | 71 | let pererror = (average - consts::E).abs() / consts::E * 100.0; | |
| 62 | 72 | let cur_percent = i as f64 / max_loops as f64 * 100.0; | |
| @@ -64,6 +74,7 @@ fn run_limited(term: Term, running: Arc<AtomicBool>, show_progress: bool, max_lo | |||
| 64 | 74 | term.move_cursor_up(1).ok(); | |
| 65 | 75 | } | |
| 66 | 76 | ||
| 77 | + | //Checks if CTRL+C was called, end loop here | |
| 67 | 78 | if !running.load(Ordering::SeqCst) { | |
| 68 | 79 | break; | |
| 69 | 80 | } | |
| @@ -78,43 +89,48 @@ fn run_limited(term: Term, running: Arc<AtomicBool>, show_progress: bool, max_lo | |||
| 78 | 89 | println!("Took: {:?}", now.elapsed()); | |
| 79 | 90 | } | |
| 80 | 91 | ||
| 81 | - | fn run_unlimited(term: Term, running: Arc<AtomicBool>, show_progress: bool) { | |
| 92 | + | fn run_unlimited(term: Term, running: Arc<AtomicBool>, log_interval: Option<u64>) { | |
| 82 | 93 | let now = Instant::now(); | |
| 83 | 94 | let mut total_count: u64 = 0; | |
| 84 | 95 | let mut count: u64; | |
| 85 | 96 | let mut sum: f64; | |
| 86 | - | let mut iteration: u64 = 0; | |
| 97 | + | let mut i: u64 = 0; | |
| 87 | 98 | ||
| 88 | - | if show_progress { | |
| 99 | + | if log_interval.is_some() { | |
| 89 | 100 | println!("Iteration\tValue \t% Err\tTime Elapsed"); | |
| 90 | 101 | } | |
| 91 | 102 | ||
| 92 | 103 | loop { | |
| 93 | 104 | count = 0; | |
| 94 | 105 | sum = 0.0; | |
| 106 | + | //Run until sum hits 1.0 | |
| 95 | 107 | while sum <= 1.0 { | |
| 96 | 108 | count += 1; | |
| 97 | 109 | sum += rand::thread_rng().gen::<f64>(); | |
| 98 | 110 | } | |
| 111 | + | ||
| 99 | 112 | total_count += count; | |
| 100 | - | if show_progress && iteration % 1000 == 0 { | |
| 101 | - | let average = total_count as f64 / iteration as f64; | |
| 113 | + | ||
| 114 | + | //Prints progress: | |
| 115 | + | if log_interval.is_some() && i % log_interval.unwrap() == 0 { | |
| 116 | + | let average = total_count as f64 / i as f64; | |
| 102 | 117 | 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(); | |
| 118 | + | term.write_line(&format!(" {:9.10}\t{:.10}\t{:5.2}%\t{:?}", i, average, pererror, now.elapsed())).ok(); | |
| 104 | 119 | term.move_cursor_up(1).ok(); | |
| 105 | 120 | } | |
| 106 | 121 | ||
| 122 | + | //Checks if CTRL+C was called, end loop here | |
| 107 | 123 | if !running.load(Ordering::SeqCst) { | |
| 108 | 124 | break; | |
| 109 | 125 | } | |
| 110 | 126 | ||
| 111 | - | iteration += 1; | |
| 127 | + | i += 1; | |
| 112 | 128 | } | |
| 113 | 129 | ||
| 114 | - | let average = total_count as f64 / iteration as f64; | |
| 130 | + | let average = total_count as f64 / i as f64; | |
| 115 | 131 | let pererror = (average - consts::E).abs() / consts::E * 100.0; | |
| 116 | 132 | println!("---------------------------"); | |
| 117 | - | println!("Iterations: {}", iteration); | |
| 133 | + | println!("Iterations: {}", i); | |
| 118 | 134 | println!("Final Average: {}", average); | |
| 119 | 135 | println!("Final % Error: {:.4}%", pererror); | |
| 120 | 136 | println!("Took: {:?}", now.elapsed()); | |
Jackz revised this gist 4 years ago. Go to revision
1 file changed, 102 insertions, 23 deletions
e.rs
| @@ -2,41 +2,120 @@ use std::time::Instant; | |||
| 2 | 2 | use rand::prelude::*; | |
| 3 | 3 | use std::f64::consts; | |
| 4 | 4 | use console::Term; | |
| 5 | - | ||
| 6 | - | const MAX_LOOPS: u64 = 1000000; | |
| 5 | + | use std::sync::atomic::{AtomicBool, Ordering}; | |
| 6 | + | use std::sync::Arc; | |
| 7 | 7 | ||
| 8 | 8 | fn main() { | |
| 9 | - | let mut total_count = 0; | |
| 10 | - | let now = Instant::now(); | |
| 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 | + | ||
| 11 | 16 | let term = Term::stdout(); | |
| 12 | - | println!(); | |
| 13 | - | for i in 0..MAX_LOOPS { | |
| 14 | - | let count = get_sum_count(); | |
| 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 | + | } | |
| 15 | 58 | total_count += count; | |
| 16 | - | if cfg!(show_msg) { | |
| 59 | + | if show_progress && i % 1000 == 0 { | |
| 17 | 60 | let average = total_count as f64 / i as f64; | |
| 18 | 61 | let pererror = (average - consts::E).abs() / consts::E * 100.0; | |
| 19 | - | let cur_percent = i as f64 / MAX_LOOPS as f64 * 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(); | |
| 20 | 64 | term.move_cursor_up(1).ok(); | |
| 21 | - | term.write_line(&format!(" {:6.10}\t{:.10}\t{:.2}% err\t{:.2}%", i, average, pererror, cur_percent)).ok(); | |
| 22 | 65 | } | |
| 23 | - | //console.log(i.toString().padEnd(10), average.toString().padEnd(20), pererror.toString().padEnd(22), "%err", " ", " ", currentPercentage + "% complete"); | |
| 66 | + | ||
| 67 | + | if !running.load(Ordering::SeqCst) { | |
| 68 | + | break; | |
| 69 | + | } | |
| 24 | 70 | } | |
| 25 | - | let average = total_count as f64 / MAX_LOOPS as f64; | |
| 71 | + | ||
| 72 | + | let average = total_count as f64 / max_loops as f64; | |
| 73 | + | let pererror = (average - consts::E).abs() / consts::E * 100.0; | |
| 26 | 74 | println!("---------------------------"); | |
| 27 | - | println!("Iterations: {}", MAX_LOOPS); | |
| 75 | + | println!("Iterations: {}", max_loops); | |
| 28 | 76 | println!("Final Average: {}", average); | |
| 29 | - | println!("Took: {:?}", now.elapsed()) | |
| 77 | + | println!("Final % Error: {:.4}%", pererror); | |
| 78 | + | println!("Took: {:?}", now.elapsed()); | |
| 30 | 79 | } | |
| 31 | 80 | ||
| 32 | - | fn get_sum_count() -> u64 { | |
| 33 | - | let mut count: u64 = 0; | |
| 34 | - | let mut sum: f64 = 0.0; | |
| 35 | - | while sum <= 1.0 { | |
| 36 | - | count += 1; | |
| 37 | - | sum += rand::thread_rng().gen::<f64>(); | |
| 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"); | |
| 38 | 90 | } | |
| 39 | - | //console.log("Count=" + currentCount + " | Sum: " + sum + " | i=" + i) | |
| 40 | - | return count; | |
| 41 | - | } | |
| 42 | 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 | + | } | |
Jackz revised this gist 4 years ago. Go to revision
1 file changed, 42 insertions
e.rs(file created)
| @@ -0,0 +1,42 @@ | |||
| 1 | + | use std::time::Instant; | |
| 2 | + | use rand::prelude::*; | |
| 3 | + | use std::f64::consts; | |
| 4 | + | use console::Term; | |
| 5 | + | ||
| 6 | + | const MAX_LOOPS: u64 = 1000000; | |
| 7 | + | ||
| 8 | + | fn main() { | |
| 9 | + | let mut total_count = 0; | |
| 10 | + | let now = Instant::now(); | |
| 11 | + | let term = Term::stdout(); | |
| 12 | + | println!(); | |
| 13 | + | for i in 0..MAX_LOOPS { | |
| 14 | + | let count = get_sum_count(); | |
| 15 | + | total_count += count; | |
| 16 | + | if cfg!(show_msg) { | |
| 17 | + | let average = total_count as f64 / i as f64; | |
| 18 | + | let pererror = (average - consts::E).abs() / consts::E * 100.0; | |
| 19 | + | let cur_percent = i as f64 / MAX_LOOPS as f64 * 100.0; | |
| 20 | + | term.move_cursor_up(1).ok(); | |
| 21 | + | term.write_line(&format!(" {:6.10}\t{:.10}\t{:.2}% err\t{:.2}%", i, average, pererror, cur_percent)).ok(); | |
| 22 | + | } | |
| 23 | + | //console.log(i.toString().padEnd(10), average.toString().padEnd(20), pererror.toString().padEnd(22), "%err", " ", " ", currentPercentage + "% complete"); | |
| 24 | + | } | |
| 25 | + | let average = total_count as f64 / MAX_LOOPS as f64; | |
| 26 | + | println!("---------------------------"); | |
| 27 | + | println!("Iterations: {}", MAX_LOOPS); | |
| 28 | + | println!("Final Average: {}", average); | |
| 29 | + | println!("Took: {:?}", now.elapsed()) | |
| 30 | + | } | |
| 31 | + | ||
| 32 | + | fn get_sum_count() -> u64 { | |
| 33 | + | let mut count: u64 = 0; | |
| 34 | + | let mut sum: f64 = 0.0; | |
| 35 | + | while sum <= 1.0 { | |
| 36 | + | count += 1; | |
| 37 | + | sum += rand::thread_rng().gen::<f64>(); | |
| 38 | + | } | |
| 39 | + | //console.log("Count=" + currentCount + " | Sum: " + sum + " | i=" + i) | |
| 40 | + | return count; | |
| 41 | + | } | |
| 42 | + | ||