Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.2.5 #14

Merged
merged 9 commits into from
Feb 1, 2024
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
26 changes: 23 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,35 @@ env:
CARGO_TERM_COLOR: always

jobs:
build:

check-format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Check formatting
run: cargo fmt -- --check

build:
runs-on: ubuntu-latest
needs: check-format
steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose

clippy:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v3
- name: Run Clippy
run: cargo clippy --all --all-features

test:
runs-on: ubuntu-latest
needs:
- clippy
steps:
- uses: actions/checkout@v3
- name: Run tests
run: cargo test --verbose -- --test-threads=1
- name: Install cargo-llvm-cov
Expand All @@ -30,4 +51,3 @@ jobs:
files: codecov.json
fail_ci_if_error: true
uses: codecov/codecov-action@v3

22 changes: 21 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Changelog

## v0.2.5 (2024-01-31)

**Improves**

* Refactors count_octal_digits to use ilog2 internally.
* Refactors count_hex_digits to use ilog2 internally.

The v0.2.4 benchmarks show that `ilog2`, used by `count_bits`, has the same performance regardless of the size of the integer input.

The v0.2.4 benchmarks show that `count_octal_digits` and `count_hex_digits` functions, which were previously using `ilog(8)` and `ilog(16)` respectively, did show decreased performance based on the size of the integer input.

![A graph of count-octal-digits benchmarks for v0.2.4](https://raw.githubusercontent.com/nordzilla/count-digits/main/benches/images/count-octal-digits-v0.2.4.png)

The v0.2.5 `count_octal_digits` and `count_hex_digits` are now implemented internally using `ilog2`, and now share that consistent, improved performance across the board.

![A graph of count-octal-digits benchmarks for v0.2.5](https://raw.githubusercontent.com/nordzilla/count-digits/main/benches/images/count-octal-digits-v0.2.5.png)

## v0.2.4 (2024-01-30)

**Adds**
Expand All @@ -9,10 +26,13 @@

**Changes**

* Removes unnecessary `checked_ilog` functions from the implementations of various `CountDigits` functions.
* Reworks testing macros to make the test output more granular.
* Reworks the benchmark test boundaries to test iterations for each hexadecimal digit added to an integer, from one to max, for each integer type.

**Improves**

* Removes unnecessary `checked_ilog` functions from the implementations of various `CountDigits` functions.

## v0.2.3 (2024-01-26)

**Changes**
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "count-digits"
version = "0.2.4"
version = "0.2.5"
authors = ["Erik Nordin <[email protected]>"]
description = "A no-std trait to count the digits of integer types in various number bases."
homepage = "https://github.com/nordzilla/count-digits"
Expand Down
8 changes: 4 additions & 4 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ impl_maybe_from_u128!(u128, NonZeroU128);
macro_rules! radix_boundaries {
($type:ty, $radix:expr) => {
std::iter::successors(Some($radix as $type), move |n| n.checked_mul($radix))
.map(|n| n - 1)
.map(|n| (n - 1) / 3)
};
}

macro_rules! comparison_bench_function {
($group:expr, $type:ty, $fn:ident, $input:expr) => {
if let Some(n) = <$type>::maybe_from_u128(*$input) {
$group.bench_with_input(
BenchmarkId::new(stringify!($type), $input),
$input,
|b, _| b.iter(|| CountDigits::$fn(black_box(n))),
BenchmarkId::new(stringify!($type), $input.count_hex_digits()),
&$input.count_hex_digits(),
|b, _| b.iter(move || CountDigits::$fn(black_box(n))),
);
}
};
Expand Down
Binary file added benches/images/count-octal-digits-v0.2.4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added benches/images/count-octal-digits-v0.2.5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 14 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
//! ---
//!
//! <div class="warning">
//! The base-10 functions
//! The base-10 functions
//! <a href="trait.CountDigits.html#tymethod.count_digits" title="method count_digits::CountDigits::count_digits">
//! count_digits()
//! </a>
Expand Down Expand Up @@ -110,9 +110,9 @@
//! assert_eq!(1, format!("{ }", -1_i8).strip_prefix('-').unwrap().chars().count());
//! assert_eq!(2, format!("{:x}", -1_i8).chars().count());
//! ```
//!
//!
//! ### Benchmarks
//!
//!
//! * [table](https://nordzilla.github.io/count-digits)
//! * [count_bits()](https://nordzilla.github.io/count-digits/count_bits/report/index.html)
//! * [count_octal_digits()](https://nordzilla.github.io/count-digits/count_octal_digits/report/index.html)
Expand Down Expand Up @@ -573,11 +573,11 @@ pub trait CountDigits: Copy + Sized {
fn count_hex_digits(self) -> u32;

/// Returns the count of digits in an integer as interpreted with the given [radix](https://en.wikipedia.org/wiki/Radix).
///
///
/// <div class="warning">
/// For radix 10, does not count the negative sign when counting negative, signed integers.
///
/// For all other radix values, counts digits according to the
///
/// For all other radix values, counts digits according to the
/// <a href="https://en.wikipedia.org/wiki/Two%27s_complement">twos-complement</a> representation.
/// </div>
///
Expand Down Expand Up @@ -630,7 +630,7 @@ macro_rules! impl_count_digits {
if self.is_negative() {
$min_value_octal_digits
} else {
1 + self.checked_ilog(8).unwrap_or_default()
1 + self.checked_ilog2().unwrap_or_default() / 3
}
}

Expand All @@ -640,7 +640,7 @@ macro_rules! impl_count_digits {
if self.is_negative() {
$min_value_hex_digits
} else {
1 + self.checked_ilog(16).unwrap_or_default()
1 + self.checked_ilog2().unwrap_or_default() / 4
}
}

Expand Down Expand Up @@ -688,7 +688,7 @@ macro_rules! impl_count_digits {
if self.is_negative() {
$min_value_octal_digits
} else {
1 + self.get().ilog(8)
1 + self.get().ilog2() / 3
}
}

Expand All @@ -698,7 +698,7 @@ macro_rules! impl_count_digits {
if self.is_negative() {
$min_value_hex_digits
} else {
1 + self.get().ilog(16)
1 + self.get().ilog2() / 4
}
}

Expand Down Expand Up @@ -743,13 +743,13 @@ macro_rules! impl_count_digits {
#[inline(always)]
/// Returns the count of octal digits in an integer starting with the first non-zero digit.
fn count_octal_digits(self) -> u32 {
1 + self.checked_ilog(8).unwrap_or_default()
1 + self.checked_ilog2().unwrap_or_default() / 3
}

#[inline(always)]
/// Returns the count of hexadecimal digits in an integer starting with the first non-zero digit.
fn count_hex_digits(self) -> u32 {
1 + self.checked_ilog(16).unwrap_or_default()
1 + self.checked_ilog2().unwrap_or_default() / 4
}

#[inline(always)]
Expand Down Expand Up @@ -783,13 +783,13 @@ macro_rules! impl_count_digits {
#[inline(always)]
/// Returns the count of octal digits in an integer starting with the first non-zero digit.
fn count_octal_digits(self) -> u32 {
1 + self.get().ilog(8)
1 + self.get().ilog2() / 3
}

#[inline(always)]
/// Returns the count of hexadecimal digits in an integer starting with the first non-zero digit.
fn count_hex_digits(self) -> u32 {
1 + self.get().ilog(16)
1 + self.get().ilog2() / 4
}

#[inline(always)]
Expand Down
Loading