-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use 'std::mem::replace' instead of 'clone'.
Replacing a value with T::zero() is usually much faster than cloning and it doesn't affect correctness.
- Loading branch information
Showing
3 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#![feature(test)] | ||
|
||
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) { | ||
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) { | ||
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) | ||
}); | ||
} | ||
|
||
#[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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use rand::RngCore; | ||
|
||
pub(crate) fn get_rng() -> impl RngCore { | ||
XorShiftStar { | ||
a: 0x0123_4567_89AB_CDEF, | ||
} | ||
} | ||
|
||
/// Simple `Rng` for benchmarking without additional dependencies | ||
struct XorShiftStar { | ||
a: u64, | ||
} | ||
|
||
impl RngCore for XorShiftStar { | ||
fn next_u32(&mut self) -> u32 { | ||
self.next_u64() as u32 | ||
} | ||
|
||
fn next_u64(&mut self) -> u64 { | ||
// https://en.wikipedia.org/wiki/Xorshift#xorshift* | ||
self.a ^= self.a >> 12; | ||
self.a ^= self.a << 25; | ||
self.a ^= self.a >> 27; | ||
self.a.wrapping_mul(0x2545_F491_4F6C_DD1D) | ||
} | ||
|
||
fn fill_bytes(&mut self, dest: &mut [u8]) { | ||
for chunk in dest.chunks_mut(8) { | ||
let bytes = self.next_u64().to_le_bytes(); | ||
let slice = &bytes[..chunk.len()]; | ||
chunk.copy_from_slice(slice) | ||
} | ||
} | ||
|
||
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> { | ||
Ok(self.fill_bytes(dest)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters