Sign in to follow this  
wforl

std::stringstream, reuse of

Recommended Posts

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();
				}

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
Ugh. Don't repeat code. Write functions.


// Of course, I'm totally guessing at certain types; adjust as needed

std::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.

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this