Skip to content

Commit

Permalink
Add benchmark to Cargo manifest.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemmih authored and cuviper committed Jun 23, 2022
1 parent ddba51a commit 275d6cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 34 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ edition = "2018"
[package.metadata.docs.rs]
features = ["std", "num-bigint-std", "serde"]

[[bench]]
name = "bigrational"

[dependencies]

[dependencies.rand]
optional = true
version = "0.8"
default-features = false

[dependencies.num-bigint]
optional = true
version = "0.4.0"
Expand Down
39 changes: 5 additions & 34 deletions benches/bigrational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,29 @@ extern crate test;

use num_bigint::BigInt;
use num_rational::{BigRational, Ratio};
// use num_traits::{FromPrimitive, Num, One, Zero};
use test::Bencher;

mod rng;
use rng::get_rng;

// allocating 'a' and 'b' is about 70ns.
// allocating BigRational(a,b) is about 970ns.
fn alloc_bench(b: &mut Bencher) {
#[bench]
fn alloc_ratio_bigint_bench(b: &mut Bencher) {
use rand::RngCore;
let mut rng = get_rng();
b.iter(|| {
let a = BigInt::from(rng.next_u64());
let b = BigInt::from(rng.next_u64());
BigRational::new(a, b)
// (a, b)
});
}

fn alloc_fast_bench(b: &mut Bencher) {
use rand::RngCore;
let mut rng = get_rng();
b.iter(|| {
let a = BigInt::from(rng.next_u64());
let b = BigInt::from(rng.next_u64());
BigRational::new_fast(a, b)
// (a, b)
});
}

fn alloc_u64_bench(b: &mut Bencher) {
#[bench]
fn alloc_ratio_u64_bench(b: &mut Bencher) {
use rand::RngCore;
let mut rng = get_rng();
b.iter(|| {
let a = rng.next_u64();
let b = rng.next_u64();
Ratio::new_fast(a, b)
// (a, b)
Ratio::new(a, b)
});
}

#[bench]
fn alloc_0(b: &mut Bencher) {
alloc_bench(b);
}

#[bench]
fn alloc_fast_0(b: &mut Bencher) {
alloc_fast_bench(b);
}

#[bench]
fn alloc_u64_0(b: &mut Bencher) {
alloc_u64_bench(b);
}

0 comments on commit 275d6cb

Please sign in to comment.