bigint.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #![feature(test)]
  2. extern crate test;
  3. extern crate num;
  4. extern crate rand;
  5. use std::mem::replace;
  6. use test::Bencher;
  7. use num::{BigInt, BigUint, Zero, One, FromPrimitive};
  8. use num::bigint::RandBigInt;
  9. use rand::{SeedableRng, StdRng};
  10. fn get_rng() -> StdRng {
  11. let seed: &[_] = &[1, 2, 3, 4];
  12. SeedableRng::from_seed(seed)
  13. }
  14. fn multiply_bench(b: &mut Bencher, xbits: usize, ybits: usize) {
  15. let mut rng = get_rng();
  16. let x = rng.gen_bigint(xbits);
  17. let y = rng.gen_bigint(ybits);
  18. b.iter(|| &x * &y);
  19. }
  20. fn divide_bench(b: &mut Bencher, xbits: usize, ybits: usize) {
  21. let mut rng = get_rng();
  22. let x = rng.gen_bigint(xbits);
  23. let y = rng.gen_bigint(ybits);
  24. b.iter(|| &x / &y);
  25. }
  26. fn factorial(n: usize) -> BigUint {
  27. let mut f: BigUint = One::one();
  28. for i in 1..(n+1) {
  29. let bu: BigUint = FromPrimitive::from_usize(i).unwrap();
  30. f = f * bu;
  31. }
  32. f
  33. }
  34. /// Compute Fibonacci numbers
  35. fn fib(n: usize) -> BigUint {
  36. let mut f0: BigUint = Zero::zero();
  37. let mut f1: BigUint = One::one();
  38. for _ in 0..n {
  39. let f2 = f0 + &f1;
  40. f0 = replace(&mut f1, f2);
  41. }
  42. f0
  43. }
  44. /// Compute Fibonacci numbers with two ops per iteration
  45. /// (add and subtract, like issue #200)
  46. fn fib2(n: usize) -> BigUint {
  47. let mut f0: BigUint = Zero::zero();
  48. let mut f1: BigUint = One::one();
  49. for _ in 0..n {
  50. f1 = f1 + &f0;
  51. f0 = &f1 - f0;
  52. }
  53. f0
  54. }
  55. #[bench]
  56. fn multiply_0(b: &mut Bencher) {
  57. multiply_bench(b, 1 << 8, 1 << 8);
  58. }
  59. #[bench]
  60. fn multiply_1(b: &mut Bencher) {
  61. multiply_bench(b, 1 << 8, 1 << 16);
  62. }
  63. #[bench]
  64. fn multiply_2(b: &mut Bencher) {
  65. multiply_bench(b, 1 << 16, 1 << 16);
  66. }
  67. #[bench]
  68. fn divide_0(b: &mut Bencher) {
  69. divide_bench(b, 1 << 8, 1 << 6);
  70. }
  71. #[bench]
  72. fn divide_1(b: &mut Bencher) {
  73. divide_bench(b, 1 << 12, 1 << 8);
  74. }
  75. #[bench]
  76. fn divide_2(b: &mut Bencher) {
  77. divide_bench(b, 1 << 16, 1 << 12);
  78. }
  79. #[bench]
  80. fn factorial_100(b: &mut Bencher) {
  81. b.iter(|| factorial(100));
  82. }
  83. #[bench]
  84. fn fib_100(b: &mut Bencher) {
  85. b.iter(|| fib(100));
  86. }
  87. #[bench]
  88. fn fib_1000(b: &mut Bencher) {
  89. b.iter(|| fib(1000));
  90. }
  91. #[bench]
  92. fn fib_10000(b: &mut Bencher) {
  93. b.iter(|| fib(10000));
  94. }
  95. #[bench]
  96. fn fib2_100(b: &mut Bencher) {
  97. b.iter(|| fib2(100));
  98. }
  99. #[bench]
  100. fn fib2_1000(b: &mut Bencher) {
  101. b.iter(|| fib2(1000));
  102. }
  103. #[bench]
  104. fn fib2_10000(b: &mut Bencher) {
  105. b.iter(|| fib2(10000));
  106. }
  107. #[bench]
  108. fn fac_to_string(b: &mut Bencher) {
  109. let fac = factorial(100);
  110. b.iter(|| fac.to_string());
  111. }
  112. #[bench]
  113. fn fib_to_string(b: &mut Bencher) {
  114. let fib = fib(100);
  115. b.iter(|| fib.to_string());
  116. }
  117. fn to_str_radix_bench(b: &mut Bencher, radix: u32) {
  118. let mut rng = get_rng();
  119. let x = rng.gen_bigint(1009);
  120. b.iter(|| x.to_str_radix(radix));
  121. }
  122. #[bench]
  123. fn to_str_radix_02(b: &mut Bencher) {
  124. to_str_radix_bench(b, 2);
  125. }
  126. #[bench]
  127. fn to_str_radix_08(b: &mut Bencher) {
  128. to_str_radix_bench(b, 8);
  129. }
  130. #[bench]
  131. fn to_str_radix_10(b: &mut Bencher) {
  132. to_str_radix_bench(b, 10);
  133. }
  134. #[bench]
  135. fn to_str_radix_16(b: &mut Bencher) {
  136. to_str_radix_bench(b, 16);
  137. }
  138. #[bench]
  139. fn to_str_radix_36(b: &mut Bencher) {
  140. to_str_radix_bench(b, 36);
  141. }
  142. fn from_str_radix_bench(b: &mut Bencher, radix: u32) {
  143. use num::Num;
  144. let mut rng = get_rng();
  145. let x = rng.gen_bigint(1009);
  146. let s = x.to_str_radix(radix);
  147. assert_eq!(x, BigInt::from_str_radix(&s, radix).unwrap());
  148. b.iter(|| BigInt::from_str_radix(&s, radix));
  149. }
  150. #[bench]
  151. fn from_str_radix_02(b: &mut Bencher) {
  152. from_str_radix_bench(b, 2);
  153. }
  154. #[bench]
  155. fn from_str_radix_08(b: &mut Bencher) {
  156. from_str_radix_bench(b, 8);
  157. }
  158. #[bench]
  159. fn from_str_radix_10(b: &mut Bencher) {
  160. from_str_radix_bench(b, 10);
  161. }
  162. #[bench]
  163. fn from_str_radix_16(b: &mut Bencher) {
  164. from_str_radix_bench(b, 16);
  165. }
  166. #[bench]
  167. fn from_str_radix_36(b: &mut Bencher) {
  168. from_str_radix_bench(b, 36);
  169. }
  170. #[bench]
  171. fn shl(b: &mut Bencher) {
  172. let n = BigUint::one() << 1000;
  173. b.iter(|| {
  174. let mut m = n.clone();
  175. for i in 0..50 {
  176. m = m << i;
  177. }
  178. })
  179. }
  180. #[bench]
  181. fn shr(b: &mut Bencher) {
  182. let n = BigUint::one() << 2000;
  183. b.iter(|| {
  184. let mut m = n.clone();
  185. for i in 0..50 {
  186. m = m >> i;
  187. }
  188. })
  189. }
  190. #[bench]
  191. fn hash(b: &mut Bencher) {
  192. use std::collections::HashSet;
  193. let mut rng = get_rng();
  194. let v: Vec<BigInt> = (1000..2000).map(|bits| rng.gen_bigint(bits)).collect();
  195. b.iter(|| {
  196. let h: HashSet<&BigInt> = v.iter().collect();
  197. assert_eq!(h.len(), v.len());
  198. });
  199. }
  200. #[bench]
  201. fn pow_bench(b: &mut Bencher) {
  202. b.iter(|| {
  203. let upper = 100_usize;
  204. for i in 2..upper + 1 {
  205. for j in 2..upper + 1 {
  206. let i_big = BigUint::from_usize(i).unwrap();
  207. num::pow(i_big, j);
  208. }
  209. }
  210. });
  211. }