-
Notifications
You must be signed in to change notification settings - Fork 246
Port over relevant changes from bigint #25
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
Changes from 3 commits
9609d2e
16012b3
ae247ae
13d7356
3356726
4336907
2af9e11
1262de4
78cf6e7
b49e0ed
85ba498
97ecf17
d3c335e
1906259
b4b5c6f
9261b85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| // Copyright 2015-2017 Parity Technologies | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
| // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
| // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
| // option. This file may not be copied, modified, or distributed | ||
| // except according to those terms. | ||
|
|
||
| //! benchmarking for bigint | ||
| //! should be started with: | ||
| //! ```bash | ||
| //! rustup run nightly cargo bench | ||
| //! ``` | ||
|
|
||
| #![feature(test)] | ||
|
|
||
| extern crate core; | ||
| extern crate test; | ||
| #[macro_use] | ||
| extern crate crunchy; | ||
| #[macro_use] | ||
| extern crate uint; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add newline before macro invocation? |
||
| construct_uint!(U128, 2); | ||
| construct_uint!(U256, 4); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use the exported types here? Apparently, creating the types here influences the benchmark results. |
||
| construct_uint!(U512, 8); | ||
|
|
||
| use test::{Bencher, black_box}; | ||
|
|
||
| impl U256 { | ||
| /// Multiplies two 256-bit integers to produce full 512-bit integer | ||
| /// No overflow possible | ||
| #[inline(always)] | ||
| pub fn full_mul(self, other: U256) -> U512 { | ||
| U512(uint_full_mul_reg!(U256, 4, self, other)) | ||
| } | ||
| } | ||
|
|
||
| #[bench] | ||
| fn u256_add(b: &mut Bencher) { | ||
| b.iter(|| { | ||
| let n = black_box(10000); | ||
| let zero = black_box(U256::zero()); | ||
| (0..n).fold(zero, |old, new| { | ||
| old.overflowing_add(U256::from(black_box(new))).0 | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn u256_sub(b: &mut Bencher) { | ||
| b.iter(|| { | ||
| let n = black_box(10000); | ||
| let max = black_box(U256::max_value()); | ||
| (0..n).fold(max, |old, new| { | ||
| old.overflowing_sub(U256::from(black_box(new))).0 | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn u512_sub(b: &mut Bencher) { | ||
| b.iter(|| { | ||
| let n = black_box(10000); | ||
| let max = black_box(U512::max_value()); | ||
| (0..n).fold(max, |old, new| { | ||
| let new = black_box(new); | ||
| let p = new % 2; | ||
| old.overflowing_sub(U512([p, p, p, p, p, p, p, new])).0 | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn u512_add(b: &mut Bencher) { | ||
| b.iter(|| { | ||
| let n = black_box(10000); | ||
| let zero = black_box(U512::zero()); | ||
| (0..n).fold(zero, |old, new| { | ||
| let new = black_box(new); | ||
| old.overflowing_add(U512([new, new, new, new, new, new, new, new])) | ||
| .0 | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn u512_mul(b: &mut Bencher) { | ||
| b.iter(|| { | ||
| (1..10000).fold(black_box(U512::one()), |old, new| { | ||
| old.overflowing_mul(U512::from(black_box(new | 1))).0 | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn u512_mul_small(b: &mut Bencher) { | ||
| b.iter(|| { | ||
| (1..153).fold(black_box(U512::one()), |old, _| { | ||
| old.overflowing_mul(U512::from(black_box(10))).0 | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn u256_mul(b: &mut Bencher) { | ||
| b.iter(|| { | ||
| (1..10000).fold(black_box(U256::one()), |old, new| { | ||
| old.overflowing_mul(U256::from(black_box(new | 1))).0 | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn u256_mul_small(b: &mut Bencher) { | ||
| b.iter(|| { | ||
| (1..77).fold(black_box(U256::one()), |old, _| { | ||
| old.overflowing_mul(U256::from(black_box(10))).0 | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn u256_full_mul(b: &mut Bencher) { | ||
| b.iter(|| { | ||
| let n = black_box(10000); | ||
| let one = black_box(U256::one()); | ||
| (1..n).map(|n| n | 1).fold(one, |old, new| { | ||
| let new = black_box(new); | ||
| let U512(ref u512words) = old.full_mul(U256([new, new, new, new])); | ||
| U256([u512words[0], u512words[2], u512words[2], u512words[3]]) | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| #[bench] | ||
| fn u128_mul(b: &mut Bencher) { | ||
| b.iter(|| { | ||
| let n = black_box(10000); | ||
| (1..n).fold(U128([12345u64, 0u64]), |old, new| { | ||
| old.overflowing_mul(U128::from(new | 1)).0 | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn u256_from_le(b: &mut Bencher) { | ||
| b.iter(|| { | ||
| let raw = black_box( | ||
| [ | ||
| 1u8, | ||
| 2, | ||
| 3, | ||
| 5, | ||
| 7, | ||
| 11, | ||
| 13, | ||
| 17, | ||
| 19, | ||
| 23, | ||
| 29, | ||
| 31, | ||
| 37, | ||
| 41, | ||
| 43, | ||
| 47, | ||
| 53, | ||
| 59, | ||
| 61, | ||
| 67, | ||
| 71, | ||
| 73, | ||
| 79, | ||
| 83, | ||
| 89, | ||
| 97, | ||
| 101, | ||
| 103, | ||
| 107, | ||
| 109, | ||
| 113, | ||
| 127, | ||
| ], | ||
| ); | ||
| let _ = U256::from_little_endian(&raw[..]); | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn u256_from_be(b: &mut Bencher) { | ||
| b.iter(|| { | ||
| let raw = black_box( | ||
| [ | ||
| 1u8, | ||
| 2, | ||
| 3, | ||
| 5, | ||
| 7, | ||
| 11, | ||
| 13, | ||
| 17, | ||
| 19, | ||
| 23, | ||
| 29, | ||
| 31, | ||
| 37, | ||
| 41, | ||
| 43, | ||
| 47, | ||
| 53, | ||
| 59, | ||
| 61, | ||
| 67, | ||
| 71, | ||
| 73, | ||
| 79, | ||
| 83, | ||
| 89, | ||
| 97, | ||
| 101, | ||
| 103, | ||
| 107, | ||
| 109, | ||
| 113, | ||
| 127, | ||
| ], | ||
| ); | ||
| let _ = U256::from_big_endian(&raw[..]); | ||
| }); | ||
| } | ||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw, repository needs an update?