Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize uint128_t::getString() #68

Merged
merged 2 commits into from Jan 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/Changelog.md
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion servus/types.h
@@ -1,4 +1,4 @@
/* Copyright (c) 2015-2016, Human Brain Project
/* Copyright (c) 2015-2017, Human Brain Project
* Stefan.Eilemann@epfl.ch
* Juan Hernando <jhernando@fi.upm.es>
*
Expand Down Expand Up @@ -56,6 +56,7 @@ class Servus;
class URI;
class uint128_t;

typedef unsigned long long ull_t;
typedef std::vector< std::string > Strings;
}

Expand Down
28 changes: 15 additions & 13 deletions servus/uint128_t.h
@@ -1,4 +1,4 @@
/* Copyright (c) 2010-2015, Cedric Stalder <cedric.stalder@gmail.com>
/* Copyright (c) 2010-2017, Cedric Stalder <cedric.stalder@gmail.com>
* Stefan Eilemann <eile@eyescale.ch>
* Daniel Nachbaur <danielnachbaur@gmail.com>
*
Expand All @@ -22,6 +22,7 @@
#define SERVUS_UINT128_H

#include <servus/api.h>
#include <servus/types.h>

#include <sstream>
#ifdef _MSC_VER
Expand Down Expand Up @@ -224,21 +225,22 @@ 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 than using std::stringstream
char buffer[ 34 ] /* 2 x 16 bytes + : + \0 */;
snprintf( buffer, 34, "%llx:%llx", ull_t( high( )), ull_t( low( )));
return std::string( buffer );
}

/** Serialize this object to a boost archive. */
template< class Archive >
Expand Down