Boost logo

Boost :

From: Alexander Nasonov (alnsn_at_[hidden])
Date: 2006-05-09 10:55:58


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

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk