Use memcpy for copying digits

This commit is contained in:
Victor Zverovich 2020-05-28 19:44:33 -07:00
parent f5fa1dee54
commit 28639969ef

View File

@ -2862,16 +2862,17 @@ class format_int {
// "Three Optimization Tips for C++". See speed-test for a comparison. // "Three Optimization Tips for C++". See speed-test for a comparison.
auto index = static_cast<unsigned>((value % 100) * 2); auto index = static_cast<unsigned>((value % 100) * 2);
value /= 100; value /= 100;
*--ptr = detail::data::digits[index + 1]; ptr -= 2;
*--ptr = detail::data::digits[index]; // memcpy is faster than copying character by character.
memcpy(ptr, detail::data::digits + index, 2);
} }
if (value < 10) { if (value < 10) {
*--ptr = static_cast<char>('0' + value); *--ptr = static_cast<char>('0' + value);
return ptr; return ptr;
} }
auto index = static_cast<unsigned>(value * 2); auto index = static_cast<unsigned>(value * 2);
*--ptr = detail::data::digits[index + 1]; ptr -= 2;
*--ptr = detail::data::digits[index]; memcpy(ptr, detail::data::digits + index, 2);
return ptr; return ptr;
} }