pub struct ProgressCounter { /* private fields */ }
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§
Source§impl ProgressCounter
impl ProgressCounter
Sourcepub fn new(total: u64) -> ProgressCounter
pub fn new(total: u64) -> ProgressCounter
Create a new ProgressCounter
that will count from 0 up to the specified total
.
Trait Implementations§
Source§impl Clone for ProgressCounter
impl Clone for ProgressCounter
Source§fn clone(&self) -> ProgressCounter
fn clone(&self) -> ProgressCounter
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl Freeze for ProgressCounter
impl RefUnwindSafe for ProgressCounter
impl Send for ProgressCounter
impl Sync for ProgressCounter
impl Unpin for ProgressCounter
impl UnwindSafe for ProgressCounter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more