diff --git a/src/cli/src/main.rs b/src/cli/src/main.rs index 5c79122..d55af8c 100644 --- a/src/cli/src/main.rs +++ b/src/cli/src/main.rs @@ -1,6 +1,6 @@ extern crate klotski_ffi; -use klotski_ffi::AllCases; +// use klotski_ffi::AllCases; // use std::thread::sleep; // use std::time::Duration; @@ -50,18 +50,20 @@ fn main() { // println!("{:09X}", code); // } - for raw_code in &AllCases::raw_codes()[..8] { - println!("{}", raw_code); - } - for short_code in &AllCases::short_codes()[..8] { - println!("{}", short_code); - } - for common_code in &AllCases::common_codes()[..8] { - println!("{}", common_code); - } + // for raw_code in &AllCases::raw_codes()[..8] { + // println!("{}", raw_code); + // } + // for short_code in &AllCases::short_codes()[..8] { + // println!("{}", short_code); + // } + // for common_code in &AllCases::common_codes()[..8] { + // println!("{}", common_code); + // } // loop { // sleep(Duration::from_secs(1)); // } + klotski_ffi::demo(); + } diff --git a/src/klotski_core/ffi/benchmark.cc b/src/klotski_core/ffi/benchmark.cc index d251fb2..cdcfa41 100644 --- a/src/klotski_core/ffi/benchmark.cc +++ b/src/klotski_core/ffi/benchmark.cc @@ -9,7 +9,7 @@ void benchmark_preparation() { Benchmark::data_preparation(); } -double benchmark_warm_up(uint64_t count) { +double benchmark_warm_up_us(uint64_t count) { return Benchmark::warm_up(count); } diff --git a/src/klotski_core/klotski.h b/src/klotski_core/klotski.h index efada34..21941f2 100644 --- a/src/klotski_core/klotski.h +++ b/src/klotski_core/klotski.h @@ -119,7 +119,7 @@ extern "C" { extern "C" { #endif EXTERN_FUNC void benchmark_preparation(); - EXTERN_FUNC double benchmark_warm_up(uint64_t count); + EXTERN_FUNC double benchmark_warm_up_us(uint64_t count); EXTERN_FUNC double benchmark_range_flip_ns(); diff --git a/src/rust_ffi/src/benchmark/chore.rs b/src/rust_ffi/src/benchmark/chore.rs new file mode 100644 index 0000000..3034eb5 --- /dev/null +++ b/src/rust_ffi/src/benchmark/chore.rs @@ -0,0 +1,58 @@ +#[derive(Debug)] +pub(crate) struct Duration { + picos: u64 +} + +impl Duration { + #[allow(dead_code)] + pub(crate) fn from_ns(ns: f64) -> Duration { + Duration { + picos: (ns * (1_000 as f64)) as u64 + } + } + + #[allow(dead_code)] + pub(crate) fn from_us(us: f64) -> Duration { + Duration { + picos: (us * (1_000_000 as f64)) as u64 + } + } + + #[allow(dead_code)] + pub(crate) fn from_ms(ms: f64) -> Duration { + Duration { + picos: (ms * (1_000_000_000 as f64)) as u64 + } + } + + #[allow(dead_code)] + pub(crate) fn from_s(s: f64) -> Duration { + Duration { + picos: (s * (1_000_000_000_000 as i64 as f64)) as u64 + } + } + + #[allow(dead_code)] + pub(crate) fn to_ns(&self) -> String { + let tmp = self.picos as f64 / (1_000 as f64); + format!("{:.03}ns", tmp) + } + + #[allow(dead_code)] + pub(crate) fn to_us(&self) -> String { + let tmp = self.picos as f64 / (1_000_000 as f64); + format!("{:.03}us", tmp) + } + + #[allow(dead_code)] + pub(crate) fn to_ms(&self) -> String { + let tmp = self.picos as f64 / (1_000_000_000 as f64); + format!("{:.03}ms", tmp) + } + + #[allow(dead_code)] + pub(crate) fn to_s(&self) -> String { + let tmp = self.picos as f64 / (1_000_000_000_000 as i64 as f64); + format!("{:.03}s", tmp) + } +} diff --git a/src/rust_ffi/src/benchmark/ffi.rs b/src/rust_ffi/src/benchmark/ffi.rs new file mode 100644 index 0000000..4e09ef8 --- /dev/null +++ b/src/rust_ffi/src/benchmark/ffi.rs @@ -0,0 +1,41 @@ +use super::Duration; +use crate::core::Core; +use std::cmp::Ordering; + +pub(crate) fn preparation() { + unsafe { + Core::benchmark_preparation(); + } +} + +pub(crate) fn warm_up(count: u64) -> Duration { + unsafe { + Duration::from_us(Core::benchmark_warm_up_us(count)) + } +} + +pub(crate) fn range_flip() -> Duration { + unsafe { + Duration::from_ns(Core::benchmark_range_flip_ns()) + } +} + +pub(crate) fn basic_ranges() -> Result { + unsafe { + let time = Core::benchmark_basic_ranges_ms(); + match time.total_cmp(&(0 as f64)) { + Ordering::Greater => Ok(Duration::from_ms(time)), + _ => Err("data already built"), + } + } +} + +pub(crate) fn all_cases() -> Result { + unsafe { + let time = Core::benchmark_all_cases_ms(); + match time.total_cmp(&(0 as f64)) { + Ordering::Greater => Ok(Duration::from_ms(time)), + _ => Err("data already built"), + } + } +} diff --git a/src/rust_ffi/src/benchmark/mod.rs b/src/rust_ffi/src/benchmark/mod.rs new file mode 100644 index 0000000..f90e1b1 --- /dev/null +++ b/src/rust_ffi/src/benchmark/mod.rs @@ -0,0 +1,18 @@ +mod ffi; + +mod chore; + +use chore::Duration; + +use ffi::*; + +pub fn demo() { + + println!("warm up: {}", warm_up(0x100_0000).to_ms()); + + println!("range flip: {}", range_flip().to_ns()); + + println!("basic ranges: {}", basic_ranges().unwrap().to_ms()); + println!("all cases: {}", all_cases().unwrap().to_ms()); + +} diff --git a/src/rust_ffi/src/lib.rs b/src/rust_ffi/src/lib.rs index f809766..d20f385 100644 --- a/src/rust_ffi/src/lib.rs +++ b/src/rust_ffi/src/lib.rs @@ -5,6 +5,7 @@ mod core; mod codec; mod metadata; mod all_cases; +mod benchmark; pub use codec::RawCode; pub use codec::ShortCode; @@ -13,3 +14,5 @@ pub use codec::CommonCode; pub use all_cases::AllCases; pub use metadata::load_metadata as metadata; + +pub use benchmark::demo;