stringstream 的一个替代

使用 ostringstream,很耗性能。 

原话是这么说的:

t's really an implementation issue, but std::locale has a static function that retrieves and set the 'global' locale. The global locale is defined to be used in several areas of the standard library which implies that there must be a global locale somewhere.

In implementations that support threads it is very likely that this global locale needs to be protected via some sort of locking mechanism to prevent simultaneous access between threads from causing undesired behaviour.

As the current standard does not explicitly deal with threading at all, it's a set implementation choices as to how (or if) this locking mechanism is implemented and whether other shared data and locks are required.

 

这里贴一个别人写的, 作为替代的作品:

template <typename T>
class PrintChar
{
public:
    static char * get() { return "%s"; }
};
template <>
class PrintChar<uint8_t>
{
public:
    static char * get() { return "%u"; }
};
template <>
class PrintChar<uint16_t>
{
public:
    static char * get() { return "%u"; }
};
template <>
class PrintChar<uint32_t>
{
public:
    static char * get() { return "%u"; }
};
template <>
class PrintChar<int8_t>
{
public:
    static char * get() { return "%d"; }
};
template <>
class PrintChar<int16_t>
{
public:
    static char * get() { return "%d"; }
};

 template <>

class PrintChar<int32_t>
{
public:
    static char * get() { return "%d"; }
};
template <>
class PrintChar<long>
{
public:
    static char * get() { return "%ld"; }
};
template <>
class PrintChar<unsigned long>
{
public:
    static char * get() { return "%lu"; }
};
template <>
class PrintChar<float>
{
public:
    static char * get() { return "%.2f"; }
};

 template <>

class PrintChar<double>
{
public:
    static char * get() { return "%.2f"; }
};
template <typename T>
class PrintChar<T *>
{
public:
    static char * get() { return "%p"; }
};
template <typename T>
class PrintChar<const T *>
{
public:
    static char * get() { return "%p"; }
};
template <>
class PrintChar<char *>
{
public:
    static char * get() { return "%s"; }
};

 template <>

class PrintChar<const char *>
{
public:
    static char * get() { return "%s"; }
};
template <typename T>
class PrintChar<const T>
{
public:
    static char * get() { return PrintChar<T>::get(); }
};
class SegString
{
public:
    SegString(size_t size, boost::pool<> *pool)
        :_size(size), _length(0), _pool(pool)
    {
        assert(_size);
        assert(_pool);
        assert(size <= _pool->get_requested_size());
        _buffer = new char[_size];
    }
    SegString(size_t size)
        :_size(size), _length(0), _pool(NULL)
    {
        assert(_size);
        _buffer = new char[_size];
    }
    ~SegString()
    {}
    void release()
    {
        if(_buffer) {
            delete [] _buffer;
            _buffer = NULL;
        }
    }
    char * get() {
        return _buffer;
    }

    template<typename T> 

  SegString& print(const T &value) {
        int ret = snprintf(&_buffer[_length], _size - _length, PrintChar<T>::get(), value);
        if(ret >= 0 && ret < int(_size - _length)) {
            _length += ret;
        }
        else if(ret >= 0) {
            _length = _size;
        }
        return *this;
    }
    SegString& print(const std::string &value) {
        return print(value.c_str());
    }
    SegString& print(const bool &value) {
        return print(value?1:0);
    }
    SegString& print(const std::vector<bool>::reference &value) {
        return print((bool)value);
    }
private:
    friend class SegStringTest;
    char * _buffer;
    size_t _size;
    size_t _length;
    boost::pool<> *_pool;

}; 

posted @ 2011-07-29 22:14  nosaferyao  阅读(789)  评论(0编辑  收藏  举报