Struct tangram_progress_counter::ProgressCounter [−][src]
pub struct ProgressCounter { /* fields omitted */ }
Expand description
A ProgressCounter
is used to efficiently track the progress of a task occurring across multiple threads.
Imagine you have the following code:
use rayon::prelude::*; let mut v: Vec<usize> = (0..1_000).collect(); v.par_iter_mut().for_each(|v| { *v += 1; });
Now you want to track the progress of this loop. You can use Arc<Mutex<T>>
like so:
use rayon::prelude::*; use std::sync::{Arc, Mutex}; let mut v: Vec<usize> = (0..1_000).collect(); let progress_counter = Arc::new(Mutex::new(0)); v.par_iter_mut().for_each(|v| { *v += 1; *progress_counter.lock().unwrap() += 1; });
However, because the work done in each loop iteration is small, a large portion of time will be spent waiting on the mutex. A better choice in this case is to use atomics. ProgressCounter
is a convenient wrapper around atomics for use in tracking progress. This example will now run much faster:
use rayon::prelude::*; use tangram_progress_counter::ProgressCounter; let mut v: Vec<usize> = (0..1_000).collect(); let progress_counter = ProgressCounter::new(v.len() as u64); v.par_iter_mut().for_each(|v| { *v += 1; progress_counter.inc(1); });
Implementations
Create a new ProgressCounter
that will count from 0 up to the specified total
.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for ProgressCounter
impl Send for ProgressCounter
impl Sync for ProgressCounter
impl Unpin for ProgressCounter
impl UnwindSafe for ProgressCounter
Blanket Implementations
Mutably borrows from an owned value. Read more