about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFalk Hüffner <falk@hueffner.de>2021-09-05 20:54:29 +0200
committerFalk Hüffner <falk@hueffner.de>2021-09-06 12:19:24 +0200
commit0c26a3bc0ce507cbc2bee0b3368664760bedcf25 (patch)
tree8a1106f4a53ab3bf77fa196291d8f3b6873e65dd
parentb2d9bcda7ec267dbabfaef67f7af344ef71e002c (diff)
downloadrust-0c26a3bc0ce507cbc2bee0b3368664760bedcf25.tar.gz
rust-0c26a3bc0ce507cbc2bee0b3368664760bedcf25.zip
Add benchmark for integer log10.
-rw-r--r--library/core/benches/lib.rs1
-rw-r--r--library/core/benches/num/int_log/mod.rs60
-rw-r--r--library/core/benches/num/mod.rs1
3 files changed, 62 insertions, 0 deletions
diff --git a/library/core/benches/lib.rs b/library/core/benches/lib.rs
index de4ef7949f3..0f55d6837eb 100644
--- a/library/core/benches/lib.rs
+++ b/library/core/benches/lib.rs
@@ -1,6 +1,7 @@
 // wasm32 does not support benches (no time).
 #![cfg(not(target_arch = "wasm32"))]
 #![feature(flt2dec)]
+#![feature(int_log)]
 #![feature(test)]
 
 extern crate test;
diff --git a/library/core/benches/num/int_log/mod.rs b/library/core/benches/num/int_log/mod.rs
new file mode 100644
index 00000000000..542a0a0ca08
--- /dev/null
+++ b/library/core/benches/num/int_log/mod.rs
@@ -0,0 +1,60 @@
+#![feature(int_log)]
+
+use rand::Rng;
+use test::{black_box, Bencher};
+
+macro_rules! int_log_bench {
+    ($t:ty, $predictable:ident, $random:ident, $random_small:ident) => {
+        #[bench]
+        fn $predictable(bench: &mut Bencher) {
+            bench.iter(|| {
+                for n in 0..(<$t>::BITS / 8) {
+                    for i in 1..=(100 as $t) {
+                        let x = black_box(i << (n * 8));
+                        black_box(x.log10());
+                    }
+                }
+            });
+        }
+
+        #[bench]
+        fn $random(bench: &mut Bencher) {
+            let mut rng = rand::thread_rng();
+            /* Exponentially distributed random numbers from the whole range of the type.  */
+            let numbers: Vec<$t> = (0..256)
+                .map(|_| {
+                    let x = rng.gen::<$t>() >> rng.gen_range(0, <$t>::BITS);
+                    if x != 0 { x } else { 1 }
+                })
+                .collect();
+            bench.iter(|| {
+                for x in &numbers {
+                    black_box(black_box(x).log10());
+                }
+            });
+        }
+
+        #[bench]
+        fn $random_small(bench: &mut Bencher) {
+            let mut rng = rand::thread_rng();
+            /* Exponentially distributed random numbers from the range 0..256.  */
+            let numbers: Vec<$t> = (0..256)
+                .map(|_| {
+                    let x = (rng.gen::<u8>() >> rng.gen_range(0, u8::BITS)) as $t;
+                    if x != 0 { x } else { 1 }
+                })
+                .collect();
+            bench.iter(|| {
+                for x in &numbers {
+                    black_box(black_box(x).log10());
+                }
+            });
+        }
+    };
+}
+
+int_log_bench! {u8, u8_log10_predictable, u8_log10_random, u8_log10_random_small}
+int_log_bench! {u16, u16_log10_predictable, u16_log10_random, u16_log10_random_small}
+int_log_bench! {u32, u32_log10_predictable, u32_log10_random, u32_log10_random_small}
+int_log_bench! {u64, u64_log10_predictable, u64_log10_random, u64_log10_random_small}
+int_log_bench! {u128, u128_log10_predictable, u128_log10_random, u128_log10_random_small}
diff --git a/library/core/benches/num/mod.rs b/library/core/benches/num/mod.rs
index 852d4e481e2..2f9cad2725d 100644
--- a/library/core/benches/num/mod.rs
+++ b/library/core/benches/num/mod.rs
@@ -1,5 +1,6 @@
 mod dec2flt;
 mod flt2dec;
+mod int_log;
 
 use std::str::FromStr;
 use test::Bencher;