Very poor boost::lexical_cast performance

后端 未结 9 901
别那么骄傲
别那么骄傲 2020-11-27 10:31

Windows XP SP3. Core 2 Duo 2.0 GHz. I\'m finding the boost::lexical_cast performance to be extremely slow. Wanted to find out ways to speed up the code. Using /O2 optimizati

相关标签:
9条回答
  • 2020-11-27 11:21

    if speed is a concern, or you are just interested in how fast such casts can be with C++, there's an interested thread regarding it.

    Boost.Spirit 2.1(which is to be released with Boost 1.40) seems to be very fast, even faster than the C equivalents(strtol(), atoi() etc. ).

    0 讨论(0)
  • 2020-11-27 11:26

    lexical_cast may or may not be as slow in relation to Java and Python as your bencharks indicate because your benchmark measurements may have a subtle problem. Any workspace allocations/deallocations done by lexical cast or the iostream methods it uses are measured by your benchmarks because C++ doesn't defer these operations. However, in the case of Java and Python, the associated deallocations may in fact have simply been deferred to a future garbage collection cycle and missed by the benchmark measurements. (Unless a GC cycle by chance occurs while the benchmark is in progress and in that case you'd be measuring too much). So it's hard to know for sure without examining specifics of the Java and Python implementations how much "cost" should be attributed to the deferred GC burden that may (or may not) be eventually imposed.

    This kind of issue obviously may apply to many other C++ vs garbage collected language benchmarks.

    0 讨论(0)
  • 2020-11-27 11:27

    I use this very fast solution for POD types...

    namespace DATATYPES {
    
        typedef std::string   TString;
        typedef char*         TCString;
        typedef double        TDouble;
        typedef long          THuge;
        typedef unsigned long TUHuge;
    };
    
    namespace boost {
    
    template<typename TYPE>
    inline const DATATYPES::TString lexical_castNumericToString(
    
                                    const TYPE& arg, 
                                    const DATATYPES::TCString fmt) {
    
        enum { MAX_SIZE = ( std::numeric_limits<TYPE>::digits10 + 1 )  // sign
                                                                + 1 }; // null
        char buffer[MAX_SIZE] = { 0 };
    
        if (sprintf(buffer, fmt, arg) < 0) {
            throw_exception(bad_lexical_cast(typeid(TYPE),
                                             typeid(DATATYPES::TString)));
        }
        return ( DATATYPES::TString(buffer) );
    }
    
    template<typename TYPE>
    inline const TYPE lexical_castStringToNumeric(const DATATYPES::TString& arg) {
    
        DATATYPES::TCString end = 0;
        DATATYPES::TDouble result = std::strtod(arg.c_str(), &end);
    
        if (not end or *end not_eq 0) {
            throw_exception(bad_lexical_cast(typeid(DATATYPES::TString),
                                             typeid(TYPE)));
        }
        return TYPE(result);
    }
    
    template<>
    inline DATATYPES::THuge lexical_cast(const DATATYPES::TString& arg) {
        return (lexical_castStringToNumeric<DATATYPES::THuge>(arg));
    }
    
    template<>
    inline DATATYPES::TString lexical_cast(const DATATYPES::THuge& arg) {
        return (lexical_castNumericToString<DATATYPES::THuge>(arg,"%li"));
    }
    
    template<>
    inline DATATYPES::TUHuge lexical_cast(const DATATYPES::TString& arg) {
        return (lexical_castStringToNumeric<DATATYPES::TUHuge>(arg));
    }
    
    template<>
    inline DATATYPES::TString lexical_cast(const DATATYPES::TUHuge& arg) {
        return (lexical_castNumericToString<DATATYPES::TUHuge>(arg,"%lu"));
    }
    
    template<>
    inline DATATYPES::TDouble lexical_cast(const DATATYPES::TString& arg) {
        return (lexical_castStringToNumeric<DATATYPES::TDouble>(arg));
    }
    
    template<>
    inline DATATYPES::TString lexical_cast(const DATATYPES::TDouble& arg) {
        return (lexical_castNumericToString<DATATYPES::TDouble>(arg,"%f"));
    }
    
    } // end namespace boost
    
    0 讨论(0)
提交回复
热议问题