From f954e1b30a7d35bb8e46de43b8b452e4662afb8b Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 22 May 2018 20:50:08 -0600 Subject: [PATCH] performancereference --- Chapter09/Cargo.toml | 4 +++ Chapter09/performance_reference.rs | 58 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 Chapter09/performance_reference.rs diff --git a/Chapter09/Cargo.toml b/Chapter09/Cargo.toml index 8644dde..fec0c89 100644 --- a/Chapter09/Cargo.toml +++ b/Chapter09/Cargo.toml @@ -63,3 +63,7 @@ path = "performance_polynomial4.rs" [[bin]] name = "performance_exponential" path = "performance_exponential.rs" + +[[bin]] +name = "performance_reference" +path = "performance_reference.rs" diff --git a/Chapter09/performance_reference.rs b/Chapter09/performance_reference.rs new file mode 100644 index 0000000..65cc73d --- /dev/null +++ b/Chapter09/performance_reference.rs @@ -0,0 +1,58 @@ +extern crate flame; +use std::fs::File; + +fn byref(n: u64, data: &[u64; 1024]) { + if n>0 { + byref(n-1, data); + byref(n-1, data); + } +} + +fn bycopy(n: u64, data: [u64; 1024]) { + if n>0 { + bycopy(n-1, data); + bycopy(n-1, data); + } +} + +struct DataClonable([u64; 1024]); +impl Clone for DataClonable { + fn clone(&self) -> Self { + let mut newdata = [0; 1024]; + for i in 0..1024 { + newdata[i] = self.0[i]; + } + DataClonable(newdata) + } +} +fn byclone(n: u64, data: T) { + if n>0 { + byclone(n-1, data.clone()); + byclone(n-1, data.clone()); + } +} + +fn main() { + let data = [0; 1024]; + flame::start("by reference"); + byref(15, &data); + flame::end("by reference"); + + let data = [0; 1024]; + flame::start("by copy"); + bycopy(15, data); + flame::end("by copy"); + + let data = [0; 1024]; + flame::start("by clone"); + byclone(15, data); + flame::end("by clone"); + + let data = DataClonable([0; 1024]); + flame::start("by clone (with extras)"); + //2^4 instead of 2^15!!!! + byclone(4, data); + flame::end("by clone (with extras)"); + + flame::dump_html(&mut File::create("flame-graph.html").unwrap()).unwrap(); +}