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
44 changes: 44 additions & 0 deletions .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: CodSpeed

on:
push:
branches: [ "main" ]
pull_request:
# `workflow_dispatch` allows CodSpeed to trigger backtest
# performance analysis in order to generate initial data.
workflow_dispatch:

permissions:
contents: read
id-token: write # for OpenID Connect authentication with CodSpeed

env:
CARGO_TERM_COLOR: always

jobs:
benchmarks:
name: Run benchmarks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.14"

- name: Setup Rust toolchain, cache and cargo-codspeed binary
uses: moonrepo/setup-rust@v0
with:
channel: stable
cache-target: release
bins: cargo-codspeed

- name: Build the benchmark targets
run: cargo codspeed build --features num-bigint

- name: Run the benchmarks
uses: CodSpeedHQ/action@v5
with:
mode: simulation
run: cargo codspeed run
15 changes: 15 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,20 @@ num-integer = { version = "0.1", optional = true }
malachite-bigint = { version = "0", optional = true } # malachite upgrades minor version a lot

[dev-dependencies]
divan = { version = "5.0.1", package = "codspeed-divan-compat" }
proptest = "1.6.0"
pyo3 = { version = "0.27", features = ["abi3", "auto-initialize"] }

[[bench]]
name = "math"
harness = false

[[bench]]
name = "cmath"
harness = false
required-features = ["complex"]

[[bench]]
name = "integer"
harness = false
required-features = ["num-bigint"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pymath
======

[![CodSpeed](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://app.codspeed.io/RustPython/pymath?utm_source=badge)

**0 ULP (bit-exact) compatibility with CPython's math and cmath modules.**

Every function produces identical results to Python at the binary representation level - not just "close enough", but exactly the same bits.
Expand Down
112 changes: 112 additions & 0 deletions benches/cmath.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//! Benchmarks for the complex-valued `cmath` module.

use divan::{Bencher, black_box};
use num_complex::Complex64;

fn main() {
divan::main();
}

/// Number of inputs evaluated per iteration.
const N: usize = 128;

/// A spiral of complex values covering all four quadrants.
fn spiral(scale: f64) -> Vec<Complex64> {
(0..N)
.map(|i| {
let t = i as f64 / N as f64 * std::f64::consts::TAU;
let r = scale * (0.25 + i as f64 / N as f64);
Complex64::new(r * t.cos(), r * t.sin())
})
.collect()
}

/// Generate one benchmark per `Complex64 -> Result<Complex64>` function.
macro_rules! bench_1 {
($($name:ident($scale:expr)),* $(,)?) => {
$(
#[divan::bench]
fn $name(bencher: Bencher) {
let zs = spiral($scale);
bencher.bench(|| {
let mut acc = Complex64::new(0.0, 0.0);
for &z in &zs {
acc += pymath::cmath::$name(black_box(z))
.unwrap_or(Complex64::new(0.0, 0.0));
}
acc
});
}
)*
};
}

bench_1! {
exp(2.0),
log10(10.0),
sqrt(10.0),
sin(2.0),
cos(2.0),
tan(2.0),
sinh(2.0),
cosh(2.0),
tanh(2.0),
asin(0.8),
acos(0.8),
atan(2.0),
asinh(2.0),
acosh(2.0),
atanh(0.8),
}

#[divan::bench]
fn log_natural(bencher: Bencher) {
let zs = spiral(10.0);
bencher.bench(|| {
let mut acc = Complex64::new(0.0, 0.0);
for &z in &zs {
acc += pymath::cmath::log(black_box(z), None).unwrap_or(Complex64::new(0.0, 0.0));
}
acc
});
}

#[divan::bench]
fn log_base(bencher: Bencher) {
let zs = spiral(10.0);
let base = Complex64::new(3.0, 0.0);
bencher.bench(|| {
let mut acc = Complex64::new(0.0, 0.0);
for &z in &zs {
acc += pymath::cmath::log(black_box(z), Some(black_box(base)))
.unwrap_or(Complex64::new(0.0, 0.0));
}
acc
});
}

#[divan::bench]
fn abs_phase(bencher: Bencher) {
let zs = spiral(10.0);
bencher.bench(|| {
let mut acc = 0.0;
for &z in &zs {
let z = black_box(z);
acc += pymath::cmath::abs(z) + pymath::cmath::phase(z).unwrap_or_default();
}
acc
});
}

#[divan::bench]
fn polar_rect(bencher: Bencher) {
let zs = spiral(10.0);
bencher.bench(|| {
let mut acc = Complex64::new(0.0, 0.0);
for &z in &zs {
let (r, phi) = pymath::cmath::polar(black_box(z)).unwrap_or((0.0, 0.0));
acc += pymath::cmath::rect(r, phi).unwrap_or(Complex64::new(0.0, 0.0));
}
acc
});
}
71 changes: 71 additions & 0 deletions benches/integer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//! Benchmarks for `math.integer`, the big-integer functions.
//!
//! Requires the `num-bigint` feature.

use divan::{Bencher, black_box};
use num_bigint::BigInt;
use pymath::math::integer;

fn main() {
divan::main();
}

/// A deterministic big integer with roughly `bits` significant bits.
fn big(bits: u32) -> BigInt {
let mut n = BigInt::from(1u32);
let mut i = 0;
while i < bits {
n = (n << 13) + BigInt::from(6_700_417u32);
i += 13;
}
n
}

const FACTORIAL_ARGS: [i64; 4] = [20, 200, 2_000, 20_000];

#[divan::bench(args = FACTORIAL_ARGS)]
fn factorial(bencher: Bencher, n: i64) {
bencher.bench(|| integer::factorial(black_box(n)));
}

const COMB_ARGS: [(i64, i64); 3] = [(30, 15), (500, 250), (5_000, 2_500)];

#[divan::bench(args = COMB_ARGS)]
fn comb(bencher: Bencher, (n, k): (i64, i64)) {
bencher.bench(|| integer::comb(black_box(n), black_box(k)));
}

#[divan::bench(args = COMB_ARGS)]
fn perm(bencher: Bencher, (n, k): (i64, i64)) {
bencher.bench(|| integer::perm(black_box(n), Some(black_box(k))));
}

const BITS: [u32; 3] = [64, 1_024, 8_192];

#[divan::bench(consts = BITS)]
fn isqrt<const B: u32>(bencher: Bencher) {
let n = big(B);
bencher.bench(|| integer::isqrt(black_box(&n)));
}

#[divan::bench(consts = BITS)]
fn gcd<const B: u32>(bencher: Bencher) {
let a = big(B);
let b = big(B) * BigInt::from(7u32) + BigInt::from(1u32);
let args = [&a, &b];
bencher.bench(|| integer::gcd(black_box(&args)));
}

#[divan::bench(consts = BITS)]
fn lcm<const B: u32>(bencher: Bencher) {
let a = big(B);
let b = big(B) * BigInt::from(7u32) + BigInt::from(1u32);
let args = [&a, &b];
bencher.bench(|| integer::lcm(black_box(&args)));
}

#[divan::bench(consts = BITS)]
fn log_bigint<const B: u32>(bencher: Bencher) {
let n = big(B);
bencher.bench(|| pymath::math::log_bigint(black_box(&n), None));
}
Loading
Loading