Opened 12 years ago

Last modified 12 years ago

#4617 new Bugs

Use of stringstreams in numeric/ublas/io

Reported by: Kostya <seraphym@…> Owned by: Gunter
Milestone: To Be Determined Component: uBLAS
Version: Boost 1.44.0 Severity: Optimization
Keywords: Cc:

Description

Not 100% sure it is a bug, but use of additional string streams seems a bit odd.

    template<class E, class T, class ME>
    // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
    std::basic_ostream<E, T> &operator << (std::basic_ostream<E, T> &os,
                                           const matrix_expression<ME> &m) {
        typedef typename ME::size_type size_type;
        size_type size1 = m ().size1 ();
        size_type size2 = m ().size2 ();
        std::basic_ostringstream<E, T, std::allocator<E> > s;
        s.flags (os.flags ());
        s.imbue (os.getloc ());
        s.precision (os.precision ());
        s << '[' << size1 << ',' << size2 << "](";
        if (size1 > 0) {
            s << '(' ;
            if (size2 > 0)
                s << m () (0, 0);
            for (size_type j = 1; j < size2; ++ j)
                s << ',' << m () (0, j);
            s << ')';
        }
        for (size_type i = 1; i < size1; ++ i) {
            s << ",(" ;
            if (size2 > 0)
                s << m () (i, 0);
            for (size_type j = 1; j < size2; ++ j)
                s << ',' << m () (i, j);
            s << ')';
        }
        s << ')';
        return os << s.str ().c_str ();
    }

why do we create stringstream 's' and serialize everything in memory and only then pump it to 'os'?

Change History (2)

comment:1 by Steven Watanabe, 12 years ago

Using a stringstream is a common way of making sure that width is handled correctly.

comment:2 by Kostya <seraphym@…>, 12 years ago

could you, please, elaborate a bit? or maybe post a link to some discussion on the matter?

Note: See TracTickets for help on using tickets.