1 #![feature(test)] 2 3 extern crate byteorder; 4 extern crate rand; 5 extern crate test; 6 7 macro_rules! bench_num { 8 ($name:ident, $read:ident, $bytes:expr, $data:expr) => ( 9 mod $name { 10 use byteorder::{ByteOrder, BigEndian, NativeEndian, LittleEndian}; 11 use super::test::Bencher; 12 use super::test::black_box as bb; 13 14 const NITER: usize = 100_000; 15 16 #[bench] 17 fn read_big_endian(b: &mut Bencher) { 18 let buf = $data; 19 b.iter(|| { 20 for _ in 0..NITER { 21 bb(BigEndian::$read(&buf, $bytes)); 22 } 23 }); 24 } 25 26 #[bench] 27 fn read_little_endian(b: &mut Bencher) { 28 let buf = $data; 29 b.iter(|| { 30 for _ in 0..NITER { 31 bb(LittleEndian::$read(&buf, $bytes)); 32 } 33 }); 34 } 35 36 #[bench] 37 fn read_native_endian(b: &mut Bencher) { 38 let buf = $data; 39 b.iter(|| { 40 for _ in 0..NITER { 41 bb(NativeEndian::$read(&buf, $bytes)); 42 } 43 }); 44 } 45 } 46 ); 47 ($ty:ident, $max:ident, 48 $read:ident, $write:ident, $size:expr, $data:expr) => ( 49 mod $ty { 50 use std::$ty; 51 use byteorder::{ByteOrder, BigEndian, NativeEndian, LittleEndian}; 52 use super::test::Bencher; 53 use super::test::black_box as bb; 54 55 const NITER: usize = 100_000; 56 57 #[bench] 58 fn read_big_endian(b: &mut Bencher) { 59 let buf = $data; 60 b.iter(|| { 61 for _ in 0..NITER { 62 bb(BigEndian::$read(&buf)); 63 } 64 }); 65 } 66 67 #[bench] 68 fn read_little_endian(b: &mut Bencher) { 69 let buf = $data; 70 b.iter(|| { 71 for _ in 0..NITER { 72 bb(LittleEndian::$read(&buf)); 73 } 74 }); 75 } 76 77 #[bench] 78 fn read_native_endian(b: &mut Bencher) { 79 let buf = $data; 80 b.iter(|| { 81 for _ in 0..NITER { 82 bb(NativeEndian::$read(&buf)); 83 } 84 }); 85 } 86 87 #[bench] 88 fn write_big_endian(b: &mut Bencher) { 89 let mut buf = $data; 90 let n = $ty::$max; 91 b.iter(|| { 92 for _ in 0..NITER { 93 bb(BigEndian::$write(&mut buf, n)); 94 } 95 }); 96 } 97 98 #[bench] 99 fn write_little_endian(b: &mut Bencher) { 100 let mut buf = $data; 101 let n = $ty::$max; 102 b.iter(|| { 103 for _ in 0..NITER { 104 bb(LittleEndian::$write(&mut buf, n)); 105 } 106 }); 107 } 108 109 #[bench] 110 fn write_native_endian(b: &mut Bencher) { 111 let mut buf = $data; 112 let n = $ty::$max; 113 b.iter(|| { 114 for _ in 0..NITER { 115 bb(NativeEndian::$write(&mut buf, n)); 116 } 117 }); 118 } 119 } 120 ); 121 } 122 123 bench_num!(u16, MAX, read_u16, write_u16, 2, [1, 2]); 124 bench_num!(i16, MAX, read_i16, write_i16, 2, [1, 2]); 125 bench_num!(u32, MAX, read_u32, write_u32, 4, [1, 2, 3, 4]); 126 bench_num!(i32, MAX, read_i32, write_i32, 4, [1, 2, 3, 4]); 127 bench_num!(u64, MAX, read_u64, write_u64, 8, [1, 2, 3, 4, 5, 6, 7, 8]); 128 bench_num!(i64, MAX, read_i64, write_i64, 8, [1, 2, 3, 4, 5, 6, 7, 8]); 129 bench_num!(f32, MAX, read_f32, write_f32, 4, [1, 2, 3, 4]); 130 bench_num!(f64, MAX, read_f64, write_f64, 8, 131 [1, 2, 3, 4, 5, 6, 7, 8]); 132 133 bench_num!(uint_1, read_uint, 1, [1]); 134 bench_num!(uint_2, read_uint, 2, [1, 2]); 135 bench_num!(uint_3, read_uint, 3, [1, 2, 3]); 136 bench_num!(uint_4, read_uint, 4, [1, 2, 3, 4]); 137 bench_num!(uint_5, read_uint, 5, [1, 2, 3, 4, 5]); 138 bench_num!(uint_6, read_uint, 6, [1, 2, 3, 4, 5, 6]); 139 bench_num!(uint_7, read_uint, 7, [1, 2, 3, 4, 5, 6, 7]); 140 bench_num!(uint_8, read_uint, 8, [1, 2, 3, 4, 5, 6, 7, 8]); 141 142 bench_num!(int_1, read_int, 1, [1]); 143 bench_num!(int_2, read_int, 2, [1, 2]); 144 bench_num!(int_3, read_int, 3, [1, 2, 3]); 145 bench_num!(int_4, read_int, 4, [1, 2, 3, 4]); 146 bench_num!(int_5, read_int, 5, [1, 2, 3, 4, 5]); 147 bench_num!(int_6, read_int, 6, [1, 2, 3, 4, 5, 6]); 148 bench_num!(int_7, read_int, 7, [1, 2, 3, 4, 5, 6, 7]); 149 bench_num!(int_8, read_int, 8, [1, 2, 3, 4, 5, 6, 7, 8]); 150 151 #[cfg(byteorder_i128)] 152 bench_num!(u128, MAX, read_u128, write_u128, 153 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); 154 #[cfg(byteorder_i128)] 155 bench_num!(i128, MAX, read_i128, write_i128, 156 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); 157 158 #[cfg(byteorder_i128)] 159 bench_num!(uint128_1, read_uint128, 160 1, [1]); 161 #[cfg(byteorder_i128)] 162 bench_num!(uint128_2, read_uint128, 163 2, [1, 2]); 164 #[cfg(byteorder_i128)] 165 bench_num!(uint128_3, read_uint128, 166 3, [1, 2, 3]); 167 #[cfg(byteorder_i128)] 168 bench_num!(uint128_4, read_uint128, 169 4, [1, 2, 3, 4]); 170 #[cfg(byteorder_i128)] 171 bench_num!(uint128_5, read_uint128, 172 5, [1, 2, 3, 4, 5]); 173 #[cfg(byteorder_i128)] 174 bench_num!(uint128_6, read_uint128, 175 6, [1, 2, 3, 4, 5, 6]); 176 #[cfg(byteorder_i128)] 177 bench_num!(uint128_7, read_uint128, 178 7, [1, 2, 3, 4, 5, 6, 7]); 179 #[cfg(byteorder_i128)] 180 bench_num!(uint128_8, read_uint128, 181 8, [1, 2, 3, 4, 5, 6, 7, 8]); 182 #[cfg(byteorder_i128)] 183 bench_num!(uint128_9, read_uint128, 184 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); 185 #[cfg(byteorder_i128)] 186 bench_num!(uint128_10, read_uint128, 187 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 188 #[cfg(byteorder_i128)] 189 bench_num!(uint128_11, read_uint128, 190 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); 191 #[cfg(byteorder_i128)] 192 bench_num!(uint128_12, read_uint128, 193 12, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); 194 #[cfg(byteorder_i128)] 195 bench_num!(uint128_13, read_uint128, 196 13, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); 197 #[cfg(byteorder_i128)] 198 bench_num!(uint128_14, read_uint128, 199 14, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); 200 #[cfg(byteorder_i128)] 201 bench_num!(uint128_15, read_uint128, 202 15, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); 203 #[cfg(byteorder_i128)] 204 bench_num!(uint128_16, read_uint128, 205 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); 206 207 #[cfg(byteorder_i128)] 208 bench_num!(int128_1, read_int128, 209 1, [1]); 210 #[cfg(byteorder_i128)] 211 bench_num!(int128_2, read_int128, 212 2, [1, 2]); 213 #[cfg(byteorder_i128)] 214 bench_num!(int128_3, read_int128, 215 3, [1, 2, 3]); 216 #[cfg(byteorder_i128)] 217 bench_num!(int128_4, read_int128, 218 4, [1, 2, 3, 4]); 219 #[cfg(byteorder_i128)] 220 bench_num!(int128_5, read_int128, 221 5, [1, 2, 3, 4, 5]); 222 #[cfg(byteorder_i128)] 223 bench_num!(int128_6, read_int128, 224 6, [1, 2, 3, 4, 5, 6]); 225 #[cfg(byteorder_i128)] 226 bench_num!(int128_7, read_int128, 227 7, [1, 2, 3, 4, 5, 6, 7]); 228 #[cfg(byteorder_i128)] 229 bench_num!(int128_8, read_int128, 230 8, [1, 2, 3, 4, 5, 6, 7, 8]); 231 #[cfg(byteorder_i128)] 232 bench_num!(int128_9, read_int128, 233 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); 234 #[cfg(byteorder_i128)] 235 bench_num!(int128_10, read_int128, 236 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 237 #[cfg(byteorder_i128)] 238 bench_num!(int128_11, read_int128, 239 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); 240 #[cfg(byteorder_i128)] 241 bench_num!(int128_12, read_int128, 242 12, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); 243 #[cfg(byteorder_i128)] 244 bench_num!(int128_13, read_int128, 245 13, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); 246 #[cfg(byteorder_i128)] 247 bench_num!(int128_14, read_int128, 248 14, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); 249 #[cfg(byteorder_i128)] 250 bench_num!(int128_15, read_int128, 251 15, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); 252 #[cfg(byteorder_i128)] 253 bench_num!(int128_16, read_int128, 254 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); 255 256 257 macro_rules! bench_slice { 258 ($name:ident, $numty:ty, $read:ident, $write:ident) => { 259 mod $name { 260 use std::mem::size_of; 261 262 use byteorder::{ByteOrder, BigEndian, LittleEndian}; 263 use rand::{self, Rng}; 264 use rand::distributions; 265 use test::Bencher; 266 267 #[bench] 268 fn read_big_endian(b: &mut Bencher) { 269 let mut numbers: Vec<$numty> = rand::thread_rng() 270 .sample_iter(&distributions::Standard) 271 .take(100000) 272 .collect(); 273 let mut bytes = vec![0; numbers.len() * size_of::<$numty>()]; 274 BigEndian::$write(&numbers, &mut bytes); 275 276 b.bytes = bytes.len() as u64; 277 b.iter(|| { 278 BigEndian::$read(&bytes, &mut numbers); 279 }); 280 } 281 282 #[bench] 283 fn read_little_endian(b: &mut Bencher) { 284 let mut numbers: Vec<$numty> = rand::thread_rng() 285 .sample_iter(&distributions::Standard) 286 .take(100000) 287 .collect(); 288 let mut bytes = vec![0; numbers.len() * size_of::<$numty>()]; 289 LittleEndian::$write(&numbers, &mut bytes); 290 291 b.bytes = bytes.len() as u64; 292 b.iter(|| { 293 LittleEndian::$read(&bytes, &mut numbers); 294 }); 295 } 296 297 #[bench] 298 fn write_big_endian(b: &mut Bencher) { 299 let numbers: Vec<$numty> = rand::thread_rng() 300 .sample_iter(&distributions::Standard) 301 .take(100000) 302 .collect(); 303 let mut bytes = vec![0; numbers.len() * size_of::<$numty>()]; 304 305 b.bytes = bytes.len() as u64; 306 b.iter(|| { 307 BigEndian::$write(&numbers, &mut bytes); 308 }); 309 } 310 311 #[bench] 312 fn write_little_endian(b: &mut Bencher) { 313 let numbers: Vec<$numty> = rand::thread_rng() 314 .sample_iter(&distributions::Standard) 315 .take(100000) 316 .collect(); 317 let mut bytes = vec![0; numbers.len() * size_of::<$numty>()]; 318 319 b.bytes = bytes.len() as u64; 320 b.iter(|| { 321 LittleEndian::$write(&numbers, &mut bytes); 322 }); 323 } 324 } 325 } 326 } 327 328 bench_slice!(slice_u64, u64, read_u64_into, write_u64_into); 329