Skip to content

Commit

Permalink
Adding file_sum_time_logs.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
simsekgokhan committed Feb 10, 2024
1 parent 907dc54 commit 038245e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
54 changes: 54 additions & 0 deletions examples/file_sum_time_logs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::fs::File;
use std::io::{BufRead, BufReader, Lines, Result};
use std::path::Path;
use std::process::exit;

/*
//// Info:
// Finds and sums the time logs in this format:
// ---c413: 0.134ms
/// Usage:
cargo r --example file_sum_time_logs ./files/time_log.txt 4121
str: 4121
count: 7 times
sum: 0.94 ms
*/

fn read_lines<P>(filename: P) -> Result<Lines<BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(BufReader::new(file).lines())
}

fn main() {
println!(
"Usage: cargo r --example file_sum_time_logs ./files/time_log.txt 4121"
);
let args: Vec<String> = std::env::args().collect();
let file = &args[1];
let find_str = &args[2];
let mut sum = 0.0;
let mut count = 0;

match read_lines(file) {
Ok(lines) => {
for line in lines.flatten() {
if !line.contains(find_str) { continue; }
let v: Vec<&str> = line.split(':').collect();
let num_str = v[1].trim().trim_end_matches("ms");
sum += num_str.parse::<f64>().unwrap_or_else(
|_| {
panic!("line: {}, num_str: {}", line, num_str)
}
);
count += 1;
}
},
Err(e) => { println!("err: {}", e); exit(-1) },
}
println!("str: {}", find_str);
println!("count: {} times", count);
println!("sum: {:.2} ms", sum);
}

2 changes: 2 additions & 0 deletions examples/hello.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Run:
// cargo r --example hello
fn main() {
let args: Vec<String> = std::env::args().collect();
println!("Hello from an example!");
println!("args: {:?}", args);
}
14 changes: 14 additions & 0 deletions files/time_log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---dataLayer: {
id: '',
name: '',
startDate: '2021-01-01',
endDate: '2029-12-01',
base: true,
readOnly: false
}
---c3: 5.922ms
---nodes.size: 148
---c4121: 0.100ms
---exit calculatePositions
---c4121: 134ms
---c4121: 0.40ms
1 change: 1 addition & 0 deletions src/file_sum_time_logs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// see examples/file_sum_time_logs.rs
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod drop_aka_dtor;
mod r#enum;
mod env_logger;
mod error_handling;
mod file_sum_time_logs;
mod func_ptr;
mod int_overflow;
mod interior_mutability;
Expand Down
2 changes: 1 addition & 1 deletion src/match_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
let x = match res {
Some(x) => x,
None => return,
};
};
println!("--- x {:?}",x);
let res: Option<i32> = None; // comment to see diff.
// Match above, can be written:
Expand Down

0 comments on commit 038245e

Please sign in to comment.