AccVec

Struct AccVec 

Source
pub struct AccVec<const S: bool, T: TraceValue, C: TraceCount>
where TDesc<S>: TArch,
{ /* private fields */ }
Expand description

An AccVec can be used to count and accumulate the times taken to execute different branches of code, from a common start point. It uses a Vec internally, and can be dynamically sized; it is otherwise very similar in operation to an AccArray.

The AccVec is generic on whether to use the CPU-specific architcture timer implementation, the value to accumulate times in (e.g. u64), the value to use to count occurrences (e.g. u32).

An AccVec can be created with a specific capacity - and memory is allocated at this time for that capacity; when it is cleared, the AccVec is emptied, but the contents are not freed.

In addition to the acc_n and acc_n_restart methods of the AccArray, the AccVec adds acc_push and acc_push_restart, which append to the current vector (and return the index used).

§Example accumulated times

An AccVec can be used to simply generate an arbitrarily long trace of accumulated elapsed times (using acc_push):

let mut t = AccVec::<true, u32,()>::default();
t.start();
// do something!
t.acc_push();
// do something more!
t.acc_push();
// do something even more!
t.acc_push();

println!("The first step took {} ticks", t.acc_cnts()[0].0);
println!("The first and second steps took {} ticks", t.acc_cnts()[1].0);
println!("The first thru third steps took {} ticks", t.acc_cnts()[2].0);

§Example individual times

An AccVec can be used to simply generate an arbitrarily long trace of elapsed times (using acc_push_restart):

let mut t = AccVec::<true, u32, u8>::default();
t.start();
// do something!
t.acc_push_restart();
// do something more!
t.acc_push_restart();
// do something even more!
t.acc_push_restart();

println!("The first step took {} ticks", t.acc_cnts()[0].0);
println!("The second step took {} ticks", t.acc_cnts()[1].0);
println!("The third step took {} ticks", t.acc_cnts()[2].0);

for ac in t.acc_cnts() {
    assert_eq!(ac.1, 1, "Each step occurred precisely once");
}

§Example accumulate times over many executions

An AccVec can be used to accumulate an arbitrarily long trace of elapsed times (using acc_push_restart):

let mut t = AccVec::<true, u32, u32>::default();

for i in 0..1000 {
  t.start();
  if i%3 == 0 {
     // do something!
  }
  t.acc_push_restart();
  if i%5 == 0 {
     // do something!
  }
  t.acc_push_restart();
  if i%2 == 0 {
     // do something!
     t.acc_push_restart();
  }
}

let ac = t.all_acc_cnts();
assert_eq!(ac.len(), 3, "There are three accumulated values");
println!("The first step took an average of {} ticks", ac[0].0 / ac[0].1);
println!("The second step took an average of {} ticks", ac[1].0 / ac[1].1);
println!("The third step took an average of {} ticks", ac[2].0 / ac[2].1);

assert_eq!(ac[0].1, 1000, "First step occurred 1000 times");
assert_eq!(ac[1].1, 1000, "Second step occurred 1000 times");
assert_eq!(ac[2].1, 500, "*Last* step occurred 500 times");

assert_eq!(t.acc_cnts().len(), 2, "The last iteration did not do the `i%2` push!");

§Example accumulate times for different operations over many executions

let mut t = AccVec::<true, u32, u32>::with_capacity(6);

for i in 0..1000 {
  t.start();
  for j in &["a", "", "bb", "ccc", "dddd", "eeeee", "bb", "ccc"] {
     let k = j.chars().count();
     t.acc_n_restart(k);
  }
}

let ac = t.all_acc_cnts();
assert_eq!(ac.len(), 6, "There are five accumulated values, the given capacity");
for i in 0..6 {
    let avg = ac[i].0 / ac[i].1;
    println!("Counting {i} characters took an average of {} ticks", avg);
}

Implementations§

Source§

impl<const S: bool, T, C> AccVec<S, T, C>
where TDesc<S>: TArch, T: TraceValue, C: TraceCount,

Source

pub fn with_capacity(n: usize) -> Self

Create a new AccVec of a certain size

Source

pub fn clear(&mut self)

Clear the timer and accumulated values

Source

pub fn start(&mut self)

Start the underlying timer

Source

pub fn acc_n(&mut self, index: usize)

Calculate the ticks elapsed, and accumulate that in the specified entry in the store

If the entry is beyond the capacity of the store, then this does nothing

Source

pub fn acc_n_restart(&mut self, index: usize)

Calculate the ticks elapsed and restart the timer, and accumulate that in the specified entry in the store

If the entry is beyond the capacity of the store, then this just restarts the timer

Source

pub fn acc_push(&mut self) -> usize

Calculate the ticks elapsed, and accumulate that in the next entry in the store

This will extend the underlying store if required

Source

pub fn acc_push_restart(&mut self) -> usize

Calculate the ticks elapsed and restart the timer, and accumulate that in the next entry in the store

This will extend the underlying store if required

Source

pub fn all_acc_cnts(&self) -> &[(T, C)]

Return all the accumulated values and counts

This should be used if the acc_n methods are used, but not acc_push

Source

pub fn acc_cnts(&self) -> &[(T, C)]

Return the accumulated values and counts, up to the last pushed

The underlying store may have more values, perhaps from previous executions. This only returns up to the last value pushed since the last start.

Trait Implementations§

Source§

impl<const S: bool, T: Clone + TraceValue, C: Clone + TraceCount> Clone for AccVec<S, T, C>
where TDesc<S>: TArch,

Source§

fn clone(&self) -> AccVec<S, T, C>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const S: bool, T: Debug + TraceValue, C: Debug + TraceCount> Debug for AccVec<S, T, C>
where TDesc<S>: TArch,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const S: bool, T, C> Default for AccVec<S, T, C>
where TDesc<S>: TArch, T: TraceValue, C: TraceCount,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const S: bool, T, C> Display for AccVec<S, T, C>
where TDesc<S>: TArch, T: TraceValue + Display + Div<C>, <T as Div<C>>::Output: Display, C: TraceCount + Display + PartialEq<C>,

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<const S: bool, T, C> !Freeze for AccVec<S, T, C>

§

impl<const S: bool, T, C> !RefUnwindSafe for AccVec<S, T, C>

§

impl<const S: bool, T, C> !Send for AccVec<S, T, C>

§

impl<const S: bool, T, C> !Sync for AccVec<S, T, C>

§

impl<const S: bool, T, C> !Unpin for AccVec<S, T, C>

§

impl<const S: bool, T, C> !UnwindSafe for AccVec<S, T, C>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.