diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2022-09-22 18:36:04 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2022-09-23 12:53:51 +0100 |
commit | ffb03fa12850df3a4f53435d5f20ff122c83732a (patch) | |
tree | bb96526f6ad40cc63d4888c4ac751e47c5f12291 | |
parent | 14d4b4fb12041dde1511262b926662929196c3fe (diff) | |
download | gcc-ffb03fa12850df3a4f53435d5f20ff122c83732a.zip gcc-ffb03fa12850df3a4f53435d5f20ff122c83732a.tar.gz gcc-ffb03fa12850df3a4f53435d5f20ff122c83732a.tar.bz2 |
libstdc++: Optimize std::bitset<N>::to_string
This makes to_string approximately twice as fast at any optimization
level. Instead of iterating through every bit, jump straight to the next
bit that is set, by using _Find_first and _Find_next.
libstdc++-v3/ChangeLog:
* include/std/bitset (bitset::_M_copy_to_string): Find set bits
instead of iterating over individual bits.
-rw-r--r-- | libstdc++-v3/include/std/bitset | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/libstdc++-v3/include/std/bitset b/libstdc++-v3/include/std/bitset index 0c84f15..83c6416 100644 --- a/libstdc++-v3/include/std/bitset +++ b/libstdc++-v3/include/std/bitset @@ -1495,9 +1495,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _CharT __zero, _CharT __one) const { __s.assign(_Nb, __zero); - for (size_t __i = _Nb; __i > 0; --__i) - if (_Unchecked_test(__i - 1)) - _Traits::assign(__s[_Nb - __i], __one); + size_t __n = this->_Find_first(); + while (__n < _Nb) + { + __s[_Nb - __n - 1] = __one; + __n = _Find_next(__n); + } } #endif // HOSTED |