about summary refs log tree commit diff
path: root/src/libcore/benches
diff options
context:
space:
mode:
authorAaron Kutch <aaronkutch@att.net>2019-08-06 10:42:48 -0500
committerAaron Kutch <aaronkutch@att.net>2019-08-06 10:42:48 -0500
commitad7fdb6859df49da179bfb4402380717aa424b0f (patch)
treec67bd730abe460bb45a802463f7858cfa0236079 /src/libcore/benches
parent890881f8f4c77e8670d4b32104c0325fcfefc90f (diff)
Improve `ptr_rotate` performance, tests, and benchmarks
Diffstat (limited to 'src/libcore/benches')
-rw-r--r--src/libcore/benches/slice.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/libcore/benches/slice.rs b/src/libcore/benches/slice.rs
index 484753c1a04..711a8dff2c0 100644
--- a/src/libcore/benches/slice.rs
+++ b/src/libcore/benches/slice.rs
@@ -55,3 +55,29 @@ fn binary_search_l2_with_dups(b: &mut Bencher) {
 fn binary_search_l3_with_dups(b: &mut Bencher) {
     binary_search(b, Cache::L3, |i| i / 16 * 16);
 }
+
+macro_rules! rotate {
+    ($fn:ident, $n:expr, $mapper:expr) => {
+        #[bench]
+        fn $fn(b: &mut Bencher) {
+            let mut x = (0usize..$n).map(&$mapper).collect::<Vec<_>>();
+            b.iter(|| {
+                for s in 0..x.len() {
+                    x[..].rotate_right(s);
+                }
+                black_box(x[0].clone())
+            })
+        }
+    };
+}
+
+#[derive(Clone)]
+struct Rgb(u8, u8, u8);
+
+rotate!(rotate_u8, 32, |i| i as u8);
+rotate!(rotate_rgb, 32, |i| Rgb(i as u8, (i as u8).wrapping_add(7), (i as u8).wrapping_add(42)));
+rotate!(rotate_usize, 32, |i| i);
+rotate!(rotate_16_usize_4, 16, |i| [i; 4]);
+rotate!(rotate_16_usize_5, 16, |i| [i; 5]);
+rotate!(rotate_64_usize_4, 64, |i| [i; 4]);
+rotate!(rotate_64_usize_5, 64, |i| [i; 5]);