std::stringstream, reuse of

Started by
4 comments, last by wforl 15 years, 7 months ago
In my code i have a whole bunch of the following uses of std::stringstream to convert data to stings and then send the string data, so some particular function that requires a string or C-string. Such as the following, every frame, a new std::stringstream is created, the precision set, some data passed into it, and then the Cstring pointer is sent to another function, and this std::stringstream is lost when the scope ends. But this is done every frame, and is done a bunch of times for all the other win32 dialog edit boxes that i fill with data, so im wondering which is better, the first example or the second, by where in the second, the same std::stringstream, is used, its just cleared.
//pixels
				std::stringstream MouseXPos2;
				static std::string  LastMouseXPos2;
				MouseXPos2.precision(2);
				MouseXPos2 << std::fixed << MousePosition.x;
				if(MouseXPos2.str() != LastMouseXPos2){
					SendMessage(GetDlgItem(HwndSide,IDC_EDIT_GRID_POINTER_X_PIXELS), WM_SETTEXT, 0, (LPARAM) MouseXPos2.str().c_str());
					LastMouseXPos2 = MouseXPos2.str();
				}

				std::stringstream MouseYPos2;
				static std::string  LastMouseYPos2;
				MouseYPos2.precision(2);
				MouseYPos2 <<  std::fixed << MousePosition.y;
				if(MouseYPos2.str() != LastMouseYPos2){
					SendMessage(GetDlgItem(HwndSide,IDC_EDIT_GRID_POINTER_Y_PIXELS), WM_SETTEXT, 0, (LPARAM) MouseYPos2.str().c_str());
					LastMouseYPos2 = MouseYPos2.str();
				}
std::stringstream MousePos;
				static std::string  LastMouseXPos, LastMouseYPos;
				MousePos.precision(2);
				MousePos << std::fixed << MousePosition.x;
				if(MousePos.str() != LastMouseXPos){
					SendMessage(GetDlgItem(HwndSide,IDC_EDIT_GRID_POINTER_X_PIXELS), WM_SETTEXT, 0, (LPARAM) MousePos.str().c_str());
					LastMouseXPos = MousePos.str();
				}

				MousePos.str("");
				MousePos <<  std::fixed << MousePosition.y;
				if(MousePos.str() != LastMouseYPos){
					SendMessage(GetDlgItem(HwndSide,IDC_EDIT_GRID_POINTER_Y_PIXELS), WM_SETTEXT, 0, (LPARAM) MousePos.str().c_str());
					LastMouseYPos = MousePos.str();
				}
Advertisement
The second example sounds better, however if this is in performance critical code then i seriously suggest not using std::stringstream. We did some benchmarks and stringstream is signficantly slower than alternatives such as atoi() and variants and also snsprintf() and variants. std::stringstream does a hell of a lot of stuff under the hood and in situations where you know exactly how something is going to be converted and you don't need a cater-for-all-data-types solution, use something other than this.

If you use any of the functions mentioned, make sure you use the safe versions (to which you specify the length of the destination buffer).

Hope that helps,
Ugh. Don't repeat code. Write functions.

// Of course, I'm totally guessing at certain types; adjust as neededstd::string asString(double value) {  std::stringstream converter;  converter.precision(2);  return (converter << std::fixed << value).str();}void sendText(int dlgIdForSpacing, const string& message) {  SendMessage(GetDlgItem(HwndSide, dlgIdForSpacing), WM_SETTEXT, 0, (LPARAM) message.c_str());}static std::string LastMouseXPos, LastMouseYPos;LastMouseXPos = asString(MousePosition.x);LastMouseYPos = asString(MousePosition.y);sendText(IDC_EDIT_GRID_POINTER_X_PIXELS, LastMouseXPos);sendText(IDC_EDIT_GRID_POINTER_Y_PIXELS, LastMouseYPos);


Or were you talking about performance?

Please, don't use atoi() unless you fully understand its behaviour and are sure it's what you want. If it receives non-numeric data, it will just return 0. There is no way to determine, just from that result, whether the data was garbage or was actually the string "0".

But if you really care, try it a few different ways, and see how the performance is as a result. In release mode, of course. And make sure the output is correct, too.
Yes, it was a discussion on performance.

I have since discovered a win32 message that actually takes an int, which is handy.

SetDlgItemInt

But im still using the stringstream for my floats,
You're using this to *configure a dialog box*, and you're worried about *performance*?
By that, i take it you mean either

1. Dialogue boxes are are slow by nature

or

2. Dialogue boxes don't need to be quick

Well, its actually a


directx game


, that im making with some extra debugging functionality.

This topic is closed to new replies.

Advertisement