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.

Retrieve the total value this ProgressCounter counts up to.

Retrieve the current progress value.

Set the current progress value.

Increment the progress value by amount.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.