Allocate vectors with capacity for faster init

This commit is contained in:
lluni 2023-02-05 18:58:20 +01:00
parent a39ac835e4
commit 0e6e15d201
Signed by: lluni
GPG key ID: ACEEB468BC325D35
2 changed files with 5 additions and 6 deletions

View file

@ -21,8 +21,8 @@ fn main() {
Uniform::new(training_interval.0, training_interval.1),
)
.to_vec();
let mut x_train = Vec::new();
let mut y_train = Vec::new();
let mut x_train = Vec::with_capacity(steps);
let mut y_train = Vec::with_capacity(steps);
for x in training_values {
x_train.push(Array1::from_elem(1usize, x));
y_train.push(Array1::from_elem(1usize, x.sin()));
@ -32,8 +32,8 @@ fn main() {
let interval_length = training_interval.1 - training_interval.0;
let step_size = interval_length / test_steps as f64;
let testing_values = Array1::range(training_interval.0, training_interval.1, step_size);
let mut x_test = Vec::new();
let mut y_test_true = Vec::new();
let mut x_test = Vec::with_capacity(test_steps);
let mut y_test_true = Vec::with_capacity(test_steps);
for x in testing_values {
x_test.push(Array1::from_elem(1usize, x));
y_test_true.push(Array1::from_elem(1usize, x.sin()));

View file

@ -26,8 +26,7 @@ impl Network {
}
pub fn predict(&mut self, inputs: &[Array1<f64>]) -> Vec<Array1<f64>> {
assert!(!inputs.is_empty());
let mut result = vec![];
let mut result = Vec::with_capacity(inputs.len());
for input in inputs.iter() {
let mut output = input.to_owned();