Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > istringstream and optimization

Reply
Thread Tools

istringstream and optimization

 
 
mathieu
Guest
Posts: n/a
 
      02-20-2006
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:stringstream os;
os.write( reinterpret_cast<char*>(&n), 4);
std::cout << n << "->" << os.str() << std::endl;

return 0;
}

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      02-20-2006
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.
 
Reply With Quote
 
 
 
 
mathieu
Guest
Posts: n/a
 
      02-20-2006
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

Mathieu

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
istringstream::str and clear Marc C++ 4 04-17-2011 11:32 AM
Zero Optimization and Sign Optimization??? Ravikiran C Programming 22 11-24-2008 03:19 AM
std::istringstream and ignore .. ma740988 C++ 5 03-31-2006 09:25 PM
new to c++... need help with istringstream and operator overloading dinks C++ 4 09-21-2004 06:02 AM
Allocating istringstream objects Samuele Armondi C++ 1 07-11-2003 10:22 PM



Advertisments