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