stringstream 的一个替代

本文介绍了一个用于提高字符串打印性能的方法,通过自定义模板类来替代使用ostringstream的方式,并提供了一个具体的实现示例。
使用 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;

}; 

转载于:https://www.cnblogs.com/nosaferyao/archive/2011/07/29/2121521.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值