mirror of https://github.com/dnomd343/klotski.git
Dnomd343
2 years ago
3 changed files with 141 additions and 36 deletions
@ -1,58 +1,98 @@ |
|||||
|
use std::fmt; |
||||
|
|
||||
#[derive(Debug)] |
#[derive(Debug)] |
||||
pub(crate) struct Duration { |
pub(crate) struct Duration { |
||||
picos: u64 |
picos: u64 |
||||
} |
} |
||||
|
|
||||
impl Duration { |
impl Duration { |
||||
#[allow(dead_code)] |
#[inline] |
||||
pub(crate) fn from_ns(ns: f64) -> Duration { |
fn to_ps(&self) -> f64 { |
||||
Duration { |
self.picos as f64 / (1 as f64) |
||||
picos: (ns * (1_000 as f64)) as u64 |
|
||||
} |
} |
||||
|
|
||||
|
#[inline] |
||||
|
fn to_ns(&self) -> f64 { |
||||
|
self.picos as f64 / (1_000 as f64) |
||||
} |
} |
||||
|
|
||||
#[allow(dead_code)] |
#[inline] |
||||
pub(crate) fn from_us(us: f64) -> Duration { |
fn to_us(&self) -> f64 { |
||||
Duration { |
self.picos as f64 / (1_000_000 as f64) |
||||
picos: (us * (1_000_000 as f64)) as u64 |
|
||||
} |
} |
||||
|
|
||||
|
#[inline] |
||||
|
fn to_ms(&self) -> f64 { |
||||
|
self.picos as f64 / (1_000_000_000 as f64) |
||||
} |
} |
||||
|
} |
||||
|
|
||||
#[allow(dead_code)] |
impl Duration { |
||||
pub(crate) fn from_ms(ms: f64) -> Duration { |
#[inline] |
||||
Duration { |
fn to_ps_str(&self) -> String { |
||||
picos: (ms * (1_000_000_000 as f64)) as u64 |
format!("{:.03}ps", self.to_ps()) |
||||
|
} |
||||
|
|
||||
|
#[inline] |
||||
|
fn to_ns_str(&self) -> String { |
||||
|
format!("{:.03}ns", self.to_ns()) |
||||
} |
} |
||||
|
|
||||
|
#[inline] |
||||
|
fn to_us_str(&self) -> String { |
||||
|
format!("{:.03}us", self.to_us()) |
||||
} |
} |
||||
|
|
||||
|
#[inline] |
||||
|
fn to_ms_str(&self) -> String { |
||||
|
format!("{:.03}ms", self.to_ms()) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
impl Duration { |
||||
|
#[inline] |
||||
#[allow(dead_code)] |
#[allow(dead_code)] |
||||
pub(crate) fn from_s(s: f64) -> Duration { |
pub(crate) fn from_ps(ps: f64) -> Duration { |
||||
Duration { |
Duration { |
||||
picos: (s * (1_000_000_000_000 as i64 as f64)) as u64 |
picos: (ps * (1 as f64)) as u64 |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
|
#[inline] |
||||
#[allow(dead_code)] |
#[allow(dead_code)] |
||||
pub(crate) fn to_ns(&self) -> String { |
pub(crate) fn from_ns(ns: f64) -> Duration { |
||||
let tmp = self.picos as f64 / (1_000 as f64); |
Duration { |
||||
format!("{:.03}ns", tmp) |
picos: (ns * (1_000 as f64)) as u64 |
||||
|
} |
||||
} |
} |
||||
|
|
||||
|
#[inline] |
||||
#[allow(dead_code)] |
#[allow(dead_code)] |
||||
pub(crate) fn to_us(&self) -> String { |
pub(crate) fn from_us(us: f64) -> Duration { |
||||
let tmp = self.picos as f64 / (1_000_000 as f64); |
Duration { |
||||
format!("{:.03}us", tmp) |
picos: (us * (1_000_000 as f64)) as u64 |
||||
|
} |
||||
} |
} |
||||
|
|
||||
|
#[inline] |
||||
#[allow(dead_code)] |
#[allow(dead_code)] |
||||
pub(crate) fn to_ms(&self) -> String { |
pub(crate) fn from_ms(ms: f64) -> Duration { |
||||
let tmp = self.picos as f64 / (1_000_000_000 as f64); |
Duration { |
||||
format!("{:.03}ms", tmp) |
picos: (ms * (1_000_000_000 as f64)) as u64 |
||||
|
} |
||||
} |
} |
||||
|
} |
||||
|
|
||||
#[allow(dead_code)] |
impl fmt::Display for Duration { |
||||
pub(crate) fn to_s(&self) -> String { |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
||||
let tmp = self.picos as f64 / (1_000_000_000_000 as i64 as f64); |
write!(f, "{}", if self.picos < 1000 { |
||||
format!("{:.03}s", tmp) |
self.to_ps_str() |
||||
|
} else if self.picos < 1_000_000 { |
||||
|
self.to_ns_str() |
||||
|
} else if self.picos < 1_000_000_000 { |
||||
|
self.to_us_str() |
||||
|
} else { |
||||
|
self.to_ms_str() |
||||
|
}) |
||||
} |
} |
||||
} |
} |
||||
|
@ -1,18 +1,35 @@ |
|||||
mod ffi; |
mod ffi; |
||||
|
|
||||
mod chore; |
mod chore; |
||||
|
|
||||
use chore::Duration; |
use chore::Duration; |
||||
|
|
||||
use ffi::*; |
|
||||
|
|
||||
pub fn demo() { |
pub fn demo() { |
||||
|
|
||||
println!("warm up: {}", warm_up(0x100_0000).to_ms()); |
use ffi::*; |
||||
|
|
||||
|
// println!("demo: {}", Duration::from_ps(233 as f64).to_string());
|
||||
|
// println!("demo: {}", Duration::from_ns(233 as f64).to_string());
|
||||
|
// println!("demo: {}", Duration::from_us(233 as f64).to_string());
|
||||
|
// println!("demo: {}", Duration::from_ms(233 as f64).to_string());
|
||||
|
// println!("demo: {}", Duration::from_ms(233000 as f64).to_string());
|
||||
|
|
||||
|
println!("warm up: {}", warm_up(0x100_0000)); |
||||
|
|
||||
|
println!("range flip: {}", range_flip()); |
||||
|
|
||||
|
println!("basic ranges: {}", basic_ranges().unwrap()); |
||||
|
println!("all cases: {}", all_cases().unwrap()); |
||||
|
|
||||
|
preparation(); |
||||
|
|
||||
|
println!("raw code check: {}", raw_code_check().unwrap()); |
||||
|
println!("short code check: {}", short_code_check().unwrap()); |
||||
|
println!("common code check: {}", common_code_check().unwrap()); |
||||
|
|
||||
println!("range flip: {}", range_flip().to_ns()); |
println!("raw code check random: {}", raw_code_check_random()); |
||||
|
println!("short code check random: {}", short_code_check_random()); |
||||
|
println!("common code check random: {}", common_code_check_random()); |
||||
|
|
||||
println!("basic ranges: {}", basic_ranges().unwrap().to_ms()); |
println!("benchmark complete"); |
||||
println!("all cases: {}", all_cases().unwrap().to_ms()); |
|
||||
|
|
||||
} |
} |
||||
|
Loading…
Reference in new issue