Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod complex;
mod dft;
mod pi;
pub use complex::*;
use core::cmp::{Eq, Ord, PartialEq, PartialOrd};
use core::ops::{Add, Div, Mul, Rem, Sub};
#[cfg(not(feature = "no_std"))]
pub use dft::*;
Expand All @@ -23,3 +24,50 @@ impl<T, Rhs, Output> NumOps<Rhs, Output> for T where
+ Rem<Rhs, Output = Output>
{
}
pub trait NumCmp<Rhs = Self>: PartialEq<Rhs> + PartialOrd<Rhs> + Eq + Ord {}
impl<T> NumCmp for T where T: PartialEq + PartialOrd + Eq + Ord {}

/// is_even
/// 判断数字是否为偶数
/// # Example
/// ```
/// use algori::math::is_even;
/// assert_eq!(is_even(12),true);
/// assert_eq!(is_even(99),false);
/// ```
pub fn is_even<T>(number: T) -> bool
where
T: NumOps + NumCmp + From<u8>,
{
number % T::from(2) == T::from(0)
}

/// is_odd
/// 判断数字是否为奇数
/// # Example
/// ```
/// use algori::math::is_odd;
/// assert_eq!(is_odd(12),false);
/// assert_eq!(is_odd(99),true);
/// ```
pub fn is_odd<T>(number: T) -> bool
where
T: NumOps + NumCmp + From<u8>,
{
!is_even(number)
}

#[cfg(test)]
mod test {
use crate::math::*;
#[test]
fn even() {
assert_eq!(is_even(12), true);
assert_eq!(is_even(11), false);
}
#[test]
fn odd() {
assert_eq!(is_odd(12), false);
assert_eq!(is_odd(11), true);
}
}
52 changes: 44 additions & 8 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,54 @@
/// use algori::test_time;
/// use algori::sorting::insertion_sort;
/// let mut a = [1,4,6,7,2,2,1,4,65,6];
/// test_time!("Insertion Sort",insertion_sort(&mut a,|a,b|a<=b));
/// test_time!("Insertion Sort",3,insertion_sort(&mut a,|a,b|a<=b));
/// ```
#[macro_export]
macro_rules! test_time {
($title:literal,$func:expr) => {
let now = std::time::Instant::now();
$func;
($title:literal, $n:expr, $func:expr) => {
let mut total_duration = std::time::Duration::new(0, 0);

for i in 0..$n {
let now = std::time::Instant::now();
$func;
let elapsed = now.elapsed();
total_duration += elapsed;

// 格式化纳秒部分为 000_000_000 的形式
let nanos = elapsed.as_nanos();
let formatted_nanos = format!("{:09}", nanos)
.chars()
.collect::<Vec<_>>()
.chunks(3)
.map(|chunk| chunk.iter().collect::<String>())
.collect::<Vec<_>>()
.join("_");

println!(
"Job:\t{}\tIteration:\t{}\tUsing\t{}\tseconds\t{}\tnanos",
$title,
i + 1,
elapsed.as_secs(),
formatted_nanos
);
}

// 计算平均耗时
let avg_secs = total_duration.as_secs() / $n;
let avg_nanos = (total_duration.as_nanos() / ($n as u128));

// 格式化平均纳秒部分为 000_000_000 的形式
let formatted_avg_nanos = format!("{:09}", avg_nanos)
.chars()
.collect::<Vec<_>>()
.chunks(3)
.map(|chunk| chunk.iter().collect::<String>())
.collect::<Vec<_>>()
.join("_");

println!(
"Job:\t{}\nUsing\t{}\tseconds\n\t{}\tnanos",
$title,
now.elapsed().as_secs(),
now.elapsed().as_nanos()
"Job:\t{}\tIterations:\t{}\tAverage Time:\t{}\tseconds\t{}\tnanos",
$title, $n, avg_secs, formatted_avg_nanos
);
};
}
Expand Down