diff --git a/Chapter09/Cargo.toml b/Chapter09/Cargo.toml index 1dbd008..7b7c7af 100644 --- a/Chapter09/Cargo.toml +++ b/Chapter09/Cargo.toml @@ -10,3 +10,23 @@ rand = "0.4.2" [[bin]] name = "performance_release_mode" path = "performance_release_mode.rs" + +[[bin]] +name = "performance_omission1" +path = "performance_omission1.rs" + +[[bin]] +name = "performance_omission2" +path = "performance_omission2.rs" + +[[bin]] +name = "performance_profiling1" +path = "performance_profiling1.rs" + +[[bin]] +name = "performance_profiling2" +path = "performance_profiling2.rs" + +[[bin]] +name = "performance_profiling3" +path = "performance_profiling3.rs" diff --git a/Chapter09/performance_omission1.rs b/Chapter09/performance_omission1.rs new file mode 100644 index 0000000..9c1f9b0 --- /dev/null +++ b/Chapter09/performance_omission1.rs @@ -0,0 +1,21 @@ +extern crate flame; +use std::fs::File; + +fn main() { + let v: Vec = vec![2; 1000000]; + + flame::start("Iterator .collect"); + let mut _z = vec![]; + for _ in 0..1000 { + _z = v.iter().map(|x| x*x).collect::>(); + } + flame::end("Iterator .collect"); + + flame::start("Iterator iterate"); + for _ in 0..1000 { + v.iter().map(|x| x * x).for_each(drop); + } + flame::end("Iterator iterate"); + + flame::dump_html(&mut File::create("flame-graph.html").unwrap()).unwrap(); +} diff --git a/Chapter09/performance_omission2.rs b/Chapter09/performance_omission2.rs new file mode 100644 index 0000000..c920c48 --- /dev/null +++ b/Chapter09/performance_omission2.rs @@ -0,0 +1,9 @@ +use std::mem::forget; + +fn main() { + for _ in 0..10000 { + let mut a = vec![2; 10000000]; + a[2] = 2; + forget(a); + } +} diff --git a/Chapter09/performance_profiling1.rs b/Chapter09/performance_profiling1.rs new file mode 100644 index 0000000..e69de29 diff --git a/Chapter09/performance_profiling2.rs b/Chapter09/performance_profiling2.rs new file mode 100644 index 0000000..e69de29 diff --git a/Chapter09/performance_profiling3.rs b/Chapter09/performance_profiling3.rs new file mode 100644 index 0000000..e69de29