473,385 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

istringstream and optimization

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::ostringstream os;
os.write( reinterpret_cast<char*>(&n), 4);
std::cout << n << "->" << os.str() << std::endl;

return 0;
}

Feb 20 '06 #1
2 1277
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.
Feb 20 '06 #2
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

Feb 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Samuele Armondi | last post by:
Hi everyone, Since istringstream objects are not assignable, I'm using the following code to allocate some dynamically. My question is: Is this the correct way of doing it? Am I deleting all the...
3
by: bml | last post by:
Could you help and answer my questions of istringstream? Thanks a lot! 1. Reuse an "istringstream" istringstream ist; ist.str("This is FIRST test string"); ist.str("This is SECOND test...
7
by: Luther Baker | last post by:
Hi, My question is regarding std::istringstream. I am serializing data to an ostringstream and the resulting buffer turns out just fine. But, when I try the reverse, when the istringstream...
4
by: dinks | last post by:
Hi I'm really new to c++ so please forgive me if this is really basic but im stuck... I am trying to make a data class that uses istringstram and overloaded << and >> operators to input and output...
6
by: JustSomeGuy | last post by:
I am passing an istringstream to a function. I want that function to get a copy of the istringstream and not a refrence to it. ie when the function returns I want the istringstream to be...
8
by: Randy Yates | last post by:
Why does this: string AWord(string& line) { return line; } bool MYOBJECT::MyFunction(string &line) { int day;
6
by: James Aguilar | last post by:
Hello all, I am trying to use an istringstream to do some input off of cin by lines. The following snippet does not work: char buf; cin.getline(buf, 90); istringstream line1(string(buf));
7
by: mathieu | last post by:
Hello, Is there a way to construct a std::istringstream from a buffer of char and avoid copying the array of bytes ? const char s = "Hello, World"; std::string str(s); std::istringstream is;...
12
by: sergey.lukoshkin | last post by:
Hello everyone! My task is in converting numbers from string to int variables. I used istringstream to perform it. So I wrote simple test function. But it doesn't work as I expected because...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.