From 6d4160cee684eb42bcbaae39f42c7238497d702c Mon Sep 17 00:00:00 2001 From: Daniel Bauer Date: Sun, 14 Oct 2018 11:53:44 +0200 Subject: [PATCH] reuse allocated string for timeseries read --- src/io.rs | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/io.rs b/src/io.rs index d1a172f..4b86ee9 100644 --- a/src/io.rs +++ b/src/io.rs @@ -129,7 +129,7 @@ fn read_window_file(window_file: &str, cfg: &Config) -> Option { eprintln!("Failed to read sample data from {}. {}", window_file, x); process::exit(1) }); - let buf = BufReader::new(&f); + let mut buf = BufReader::new(&f); // total number of bins is the product of all dimensions length let total_bins = cfg.num_bins.iter().fold(1, |s, &x| { s*x }); @@ -141,30 +141,32 @@ fn read_window_file(window_file: &str, cfg: &Config) -> Option { }).collect(); // read and parse each timeseries line - for l in buf.lines() { - let line = l.unwrap(); + let mut line = String::new(); + while buf.read_line(&mut line).unwrap() > 0 { + { + // skip comments and empty lines + if line.starts_with("#") || line.starts_with("@") || line.len() == 0 { + continue; + } - // skip comments and empty lines - if line.starts_with("#") || line.starts_with("@") || line.len() == 0 { - continue; - } - - let mut split = line.split_whitespace(); - split.next(); // skip time/step column + let mut split = line.split_whitespace(); + split.next(); // skip time/step column - let values: Vec = (0..cfg.dimens).collect::>().iter().map(|_| { - split.next().unwrap().parse::().unwrap() - }).collect(); - - if is_in_hist_boundaries(&values, cfg) { - let bin_indeces = (0..cfg.dimens).map(|dimen: usize| { - let val = values[dimen]; - ((val-cfg.hist_min[dimen]) / bin_width[dimen]) as usize + let values: Vec = (0..cfg.dimens).collect::>().iter().map(|_| { + split.next().unwrap().parse::().unwrap() }).collect(); - let index = flat_index(&bin_indeces, &cfg.num_bins); - hist[index] += 1.0; + + if is_in_hist_boundaries(&values, cfg) { + let bin_indeces = (0..cfg.dimens).map(|dimen: usize| { + let val = values[dimen]; + ((val - cfg.hist_min[dimen]) / bin_width[dimen]) as usize + }).collect(); + let index = flat_index(&bin_indeces, &cfg.num_bins); + hist[index] += 1.0; + } } + line.clear(); } let num_points: f64 = hist.iter().sum();