diff --git a/doc/Changelog.md b/doc/Changelog.md index 7f26b93..0582f2f 100644 --- a/doc/Changelog.md +++ b/doc/Changelog.md @@ -3,6 +3,9 @@ # git master +* [68](https://github.com/HBPVis/Servus/pull/68): + Optimize uint128_t::getString() + # Release 1.5 (09-12-2016) * [64](https://github.com/HBPVis/Servus/pull/64): diff --git a/servus/uint128_t.h b/servus/uint128_t.h index 2f8f08c..2baa2e2 100644 --- a/servus/uint128_t.h +++ b/servus/uint128_t.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2010-2015, Cedric Stalder +/* Copyright (c) 2010-2016, Cedric Stalder * Stefan Eilemann * Daniel Nachbaur * @@ -224,21 +224,26 @@ class uint128_t /** @return a short, but not necessarily unique, string of the value. */ std::string getShortString() const - { - std::stringstream stream; - stream << std::hex << _high << _low; - const std::string str = stream.str(); - return str.substr( 0, 3 ) + ".." + - str.substr( str.length() - 3, std::string::npos ); - } + { + std::stringstream stream; + stream << std::hex << _high << _low; + const std::string str = stream.str(); + return str.substr( 0, 3 ) + ".." + + str.substr( str.length() - 3, std::string::npos ); + } /** @return the full string representation of the value. */ std::string getString() const - { - std::stringstream stream; - stream << *this; - return stream.str(); - } + { + // OPT: snprintf is faster then using std::stringstream + char buffer[ 34 ] /* 16 bytes + : + \0 */; +#ifdef _MSC_VER + snprintf( buffer, 34, "%llx:%llx", high(), low( )); +#else + snprintf( buffer, 34, "%lx:%lx", high(), low( )); +#endif + return std::string( buffer ); + } /** Serialize this object to a boost archive. */ template< class Archive >