InputBuilder

Struct InputBuilder 

Source
pub struct InputBuilder<I: Input, D: Distribution> { /* private fields */ }
Expand description

Struct used for building an InputSet.

Implementations§

Source§

impl<I: Input, D: Distribution> InputBuilder<I, D>

Source

pub fn new(distribution: D, builder: I::Builder) -> InputBuilder<I, D>

Creates a new InputBuilder.

§Arguments
  • distribution - The distribution that will be used to generate the input lengths.
  • builder - The builder that will be used to generate the inputs.
Examples found in repository?
examples/sorting/main.rs (line 23)
17fn main() {
18    // Create a distribution for the length of the vectors
19    // Here we use an exponential distribution with a minimum of 1000 and a maximum of 500_000
20    let length_distribution = Exponential::new(1000..=500_000);
21
22    // Create the builder for the vectors
23    let vector_builder = InputBuilder::new(length_distribution, ());
24
25    // Build the vectors
26    // Here we build 2000 vectors, 10 of each length
27    let mut vectors = vector_builder.build_with_repetitions(200, 10);
28
29    // Create a slice of the algorithms we want to measure
30    let algorithms: &[(fn(&mut input::InputVec), &str); 2] = &[
31        (merge_sort_input, "Merge sort"),
32        (quick_sort_input, "Quick sort"),
33    ];
34
35    // Measure the algorithms on the vectors, given a relative error of 0.001
36    let results = measure_mut(&mut vectors, algorithms, 0.001);
37
38    let result_clone = results.clone();
39    // Serialize the results to a json file
40    result_clone.serialize_json("results.json");
41
42    let file_name = "results/sorting.svg";
43
44    // Plot the results
45    let config = PlotConfig::default()
46        .with_title("Sorting algorithms")
47        .with_caption("The time plot of sorting algorithms");
48
49    time_plot(file_name, results, &config);
50}
More examples
Hide additional examples
examples/search/main.rs (line 26)
19fn main() {
20    // Create a distribution for the length of the vectors
21    // Here we use an uniform distribution with a minimum of 10 and a maximum of 100_000
22    let length_distribution = Uniform::new(10..=100_000);
23
24    // Create the builder for the vectors
25    // Here we choose to use the fast generator method in order to generate ordered vectors
26    let vector_builder = InputBuilder::new(length_distribution, Generator::Fast);
27
28    // Build 200 vectors
29    let vectors = vector_builder.build(200);
30
31    // Create a slice of the algorithms we want to measure
32    let algorithms: &[(fn(&input::SearchInput) -> Option<usize>, &str); 2] = &[
33        (linear_search_input, "Linear search"),
34        (binary_search_input, "Binary search"),
35    ];
36
37    // Measure the algorithms on the vectors, given a relative error of 0.001
38    let results = measure(&vectors, algorithms, 0.001);
39
40    let file_name = "results/search.svg";
41
42    // Here we print the linear regression of the log-log scale of the results
43    for result in results.clone().measurements {
44        let log_linear_regression = result.log_log_scale().linear_regression();
45        println!(
46            "{}: {} * x + {}",
47            result.algorithm_name, log_linear_regression.0, log_linear_regression.1
48        )
49    }
50
51    let config = PlotConfig::default()
52        .with_title("Search in an ordered vector")
53        .with_caption("The time plot of searching algorithms in an ordered vector");
54
55    // Plot the results
56    time_plot(file_name, results, &config);
57}
Source

pub fn build(&self, n: usize) -> InputSet<I>

Generates the inputs.

§Arguments
  • n - The number of inputs to be generated.
Examples found in repository?
examples/search/main.rs (line 29)
19fn main() {
20    // Create a distribution for the length of the vectors
21    // Here we use an uniform distribution with a minimum of 10 and a maximum of 100_000
22    let length_distribution = Uniform::new(10..=100_000);
23
24    // Create the builder for the vectors
25    // Here we choose to use the fast generator method in order to generate ordered vectors
26    let vector_builder = InputBuilder::new(length_distribution, Generator::Fast);
27
28    // Build 200 vectors
29    let vectors = vector_builder.build(200);
30
31    // Create a slice of the algorithms we want to measure
32    let algorithms: &[(fn(&input::SearchInput) -> Option<usize>, &str); 2] = &[
33        (linear_search_input, "Linear search"),
34        (binary_search_input, "Binary search"),
35    ];
36
37    // Measure the algorithms on the vectors, given a relative error of 0.001
38    let results = measure(&vectors, algorithms, 0.001);
39
40    let file_name = "results/search.svg";
41
42    // Here we print the linear regression of the log-log scale of the results
43    for result in results.clone().measurements {
44        let log_linear_regression = result.log_log_scale().linear_regression();
45        println!(
46            "{}: {} * x + {}",
47            result.algorithm_name, log_linear_regression.0, log_linear_regression.1
48        )
49    }
50
51    let config = PlotConfig::default()
52        .with_title("Search in an ordered vector")
53        .with_caption("The time plot of searching algorithms in an ordered vector");
54
55    // Plot the results
56    time_plot(file_name, results, &config);
57}
Source

pub fn build_with_repetitions( &self, n: usize, repetitions: usize, ) -> InputSet<I>

Generates the inputs with repetitions (i.e. multiple inputs with the same size). This can be useful in order to obtain a more accurate result.

§Arguments
  • n - The number of inputs to be generated (excluding repetitions: the actual amount of inputs generated is n*repetitions).
  • repetitions - The number of repetitions for each input size.
Examples found in repository?
examples/sorting/main.rs (line 27)
17fn main() {
18    // Create a distribution for the length of the vectors
19    // Here we use an exponential distribution with a minimum of 1000 and a maximum of 500_000
20    let length_distribution = Exponential::new(1000..=500_000);
21
22    // Create the builder for the vectors
23    let vector_builder = InputBuilder::new(length_distribution, ());
24
25    // Build the vectors
26    // Here we build 2000 vectors, 10 of each length
27    let mut vectors = vector_builder.build_with_repetitions(200, 10);
28
29    // Create a slice of the algorithms we want to measure
30    let algorithms: &[(fn(&mut input::InputVec), &str); 2] = &[
31        (merge_sort_input, "Merge sort"),
32        (quick_sort_input, "Quick sort"),
33    ];
34
35    // Measure the algorithms on the vectors, given a relative error of 0.001
36    let results = measure_mut(&mut vectors, algorithms, 0.001);
37
38    let result_clone = results.clone();
39    // Serialize the results to a json file
40    result_clone.serialize_json("results.json");
41
42    let file_name = "results/sorting.svg";
43
44    // Plot the results
45    let config = PlotConfig::default()
46        .with_title("Sorting algorithms")
47        .with_caption("The time plot of sorting algorithms");
48
49    time_plot(file_name, results, &config);
50}

Trait Implementations§

Source§

impl<I: Input, D> Serialize for InputBuilder<I, D>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<I, D> Freeze for InputBuilder<I, D>
where D: Freeze, <I as Input>::Builder: Freeze,

§

impl<I, D> RefUnwindSafe for InputBuilder<I, D>

§

impl<I, D> Send for InputBuilder<I, D>
where D: Send, <I as Input>::Builder: Send,

§

impl<I, D> Sync for InputBuilder<I, D>
where D: Sync, <I as Input>::Builder: Sync,

§

impl<I, D> Unpin for InputBuilder<I, D>
where D: Unpin, <I as Input>::Builder: Unpin,

§

impl<I, D> UnwindSafe for InputBuilder<I, D>
where D: UnwindSafe, <I as Input>::Builder: UnwindSafe,

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> 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, 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V