fast mul by 10

This commit is contained in:
debris 2015-12-12 13:32:42 +01:00
parent 862702b9c7
commit 38f813a5e6
1 changed files with 7 additions and 2 deletions

View File

@ -383,8 +383,13 @@ macro_rules! construct_uint {
/// TODO: optimize, throw appropriate err
fn from_dec_str(value: &str) -> Result<Self, Self::Err> {
let ten = $name::from(10u64);
Ok(value.bytes().map(|b| b - 48).fold($name::from(0u64), | acc, c | acc * ten + $name::from(c) ))
Ok(value.bytes()
.map(|b| b - 48)
.fold($name::from(0u64), | acc, c |
// fast multiplication by 10
// (acc << 3) + (acc << 1) => acc * 10
(acc << 3) + (acc << 1) + $name::from(c)
))
}
}