istringstream and optimization

Discussion in 'C++' started by mathieu, Feb 20, 2006.

  1. mathieu

    mathieu Guest

    Hello there,

    Could anyone comment on the following code(*). I would like to
    optimize the following line:

    is.str( std::string(s,4) );

    Seems to me that allocation+initialization of the std::string is a
    bit complex for such simple task. Looks to me that I should be able to
    manipulate the rdbuf internals.

    Comments welcome, thanks.
    Mathieu

    (*)
    #include <iostream>
    #include <sstream>

    int main()
    {
    // 9728
    const char s[] = "\0&\0\0";
    uint32_t n = 0;
    memcpy(&n, s, 4);
    std::cout << n << std::endl;

    uint32_t nn = 0;
    std::istringstream is;
    is.str( std::string(s,4) ); // <----- this line
    is.read( reinterpret_cast<char*>(&nn), 4);
    std::cout << is.str() << "->" << nn << std::endl;

    std::eek:stringstream os;
    os.write( reinterpret_cast<char*>(&n), 4);
    std::cout << n << "->" << os.str() << std::endl;

    return 0;
    }
     
    mathieu, Feb 20, 2006
    #1
    1. Advertising

  2. mathieu

    red floyd Guest

    mathieu wrote:
    > Hello there,
    >
    > Could anyone comment on the following code(*). I would like to
    > optimize the following line:
    >
    > is.str( std::string(s,4) );
    >
    > Seems to me that allocation+initialization of the std::string is a
    > bit complex for such simple task. Looks to me that I should be able to
    > manipulate the rdbuf internals.
    >

    Have you benchmarked to see if this actually is a problem? What is the
    obsession that people seem to have with premature optimization, without
    regard to Hoare's/Knuth's Law ("Premature optimization is the root of
    all evil")?

    Manipulating the rdbuf internals will most likely lead to less
    understandable and therefore less maintainable code.

    I'd code for what my last job used to call the "X-abilities"
    (reliability, scalability, maintainability), and worry about
    micromanaging performance later, after testing and benchmarks indicate a
    problem.

    But that's just my opinion.
     
    red floyd, Feb 20, 2006
    #2
    1. Advertising

  3. mathieu

    mathieu Guest

    And the solution is...that you are indeed right !
    I was going back and forth in my istringstream/ostringstream. Redoing
    my templates I was able to skip this step :p

    Mathieu
     
    mathieu, Feb 20, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samuele Armondi

    Allocating istringstream objects

    Samuele Armondi, Jul 11, 2003, in forum: C++
    Replies:
    1
    Views:
    383
    Samuele Armondi
    Jul 11, 2003
  2. dinks
    Replies:
    4
    Views:
    2,028
    dinks
    Sep 21, 2004
  3. ma740988
    Replies:
    5
    Views:
    1,589
  4. Ravikiran

    Zero Optimization and Sign Optimization???

    Ravikiran, Nov 17, 2008, in forum: C Programming
    Replies:
    22
    Views:
    904
    Thad Smith
    Nov 24, 2008
  5. Marc
    Replies:
    4
    Views:
    611
Loading...

Share This Page