[string_convert] efficiency of to_string_classic

I played with to_string_classic from vault / Strings - Text Processing / string_convert.zip It's faster then ostringstream but not much: Intel 8.1.028 on FreeBSD 6.1, -O2: ostream-based implementations (best of three runs) | ---------------------------------------------------- to_string_classic | 26.091 seconds | ---------------------------------------------------- ostringstream | 32.335 seconds | ---------------------------------------------------- GNU gcc 3.4.4 on FreeBSD 6.1, -O2: ostream-based implementations (best of three runs) | ---------------------------------------------------- to_string_classic | 25.126 seconds | ---------------------------------------------------- ostringstream | 31.333 seconds | ---------------------------------------------------- This is very slow compared to integral2str function defined in vault / Strings - Text Processing / integral2str.hpp Unlike to_string_classic which returns std::string, integral2str function returns boost::array<char,N>. I wrap it into std::string for more accurate comparison with to_string_classic. Here are the results: Intel 8.1.028 on FreeBSD 6.1, -O2: integral2str (worst of three runs) | ------------------------------------ std::string | 5.690 seconds | ------------------------------------ boost::array | 0.734 seconds | ------------------------------------ GNU gcc 3.4.4 on FreeBSD 6.1, -O2: integral2str (worst of three runs) | ------------------------------------ std::string | 5.763 seconds | ------------------------------------ boost::array | 1.247 seconds | ------------------------------------ // // Code that measure performance of ostream-based implementations // #include "to_string.hpp" #include <locale> #include <sstream> std::string slow(int n) { std::ostringstream out; out.imbue(std::locale::classic()); out << n; return out.str(); } int main() { using namespace string_convert; char volatile x = 0; for(int i = 0; i < 10000000; ++i) x += to_string_classic(i)[i % 2]; //x += slow(i)[i % 2]; return x; } // // Code that measure performance of integral2str // #include "integral2str.hpp" #include <string> int main() { char volatile x = 0; for(int i = 0; i < 10000000; ++i) x += integral2str(i)[i % 2]; //x += std::string(integral2str(i).data())[i % 2]; return x; } -- Alexander Nasonov Project Manager http://www.akmosoft.com

Alexander Nasonov <alnsn <at> yandex.ru> writes:
I played with to_string_classic from
vault / Strings - Text Processing / string_convert.zip
It's faster then ostringstream but not much:
That is expected since the default implementation of to_string_classic use streams. Any speedup is due to my special stream implementation which avoids some memory allocations and string copies (20% faster is more than I expected) The idea behind the to_string_classic is that you can specialize it for common types. Something you can't do with e.g. lexical_cast since it uses the default locale. (The to_string_classic interface might need to be changed to allow specialization though, never tested it) You could also check the figures with the string_convert class where the stream is reused. string_convert conv<std::string>(std::locale::classic()); for(int i = 0; i < 10000000; ++i) x += conv.to_string(i)[i % 2]; Nice to know that my library works on both intel and gcc
participants (2)
-
Alexander Nasonov
-
Martin Adrian