Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualServant of the Lord

Posted 27 September 2012 - 10:36 PM

Yeah, stringstream doesn't take 2 seconds to do a single conversion. It doesn't even take 2 millaseconds for a single conversion, so I'm not sure where that data came from, but in my own experience it's inaccurate. Yes, a hand-rolled integer conversion would be faster in most cases, so you are doing less (it's less general), but premature optimization is not good. Use hand-rolled where you need speed, otherwise don't re-invent the wheel.

At no point should stringstream, atoi, etc. have anything to do with it.

Quoted for truth - look at Cornstalk's post. The only place int to string (not vise-versa) is needed, is for putting the results in a new string if the OP desires.

I'd do it something like this:
Cast string to lowercase. (std::transform, and std::tolower)
Iterate over each char in string (just index into it)
I'd cast the char to an int
//Since ASCII lowercase letters begins at 97, and goes to 122, and space is 32...
if the int is 32

. . . .output a '-' to the result, if desired, or leave it as a space, or discard it.
if the int is over 122 or under 97

. . . .output a ? to the result, or discard that char
otherwise
. . . . Subtract 96, so 'a' becomes '1', and so on.

#2Servant of the Lord

Posted 27 September 2012 - 10:36 PM

Yeah, stringstream doesn't take 2 seconds to do a single conversion. It doesn't even take 2 millaseconds for a single conversion, so I'm not sure where that data came from, but in my own experience it's inaccurate. Yes, a hand-rolled integer conversion would be faster in most cases, so you are doing less (it's less general), but premature optimization is not good. Use hand-rolled where you need speed, otherwise don't re-invent the wheel.

At no point should stringstream, atoi, etc. have anything to do with it.

Quoted for truth - look at Cornstalk's post. The only place int to string (not vise-versa) is needed, is for putting the results in a new string if the OP desires.

I'd do it something like this:
Cast string to lowercase. (std::transform, and std::tolower)
Iterate over each char in string (just index into it)
I'd cast the char to an int
//Since ASCII lowercase letters begins at 97, and goes to 122, and space is 32...
if the int is 32
output a '-' to the result, if desired, or leave it as a space, or discard it.
if the int is over 122 or under 97
output a ? to the result, or discard that char
otherwise
Subtract 96, so 'a' becomes '1', and so on.

#1Servant of the Lord

Posted 27 September 2012 - 10:36 PM

Yeah, stringstream doesn't take 2 seconds to do a single conversion. It doesn't even take 2 millaseconds for a single conversion, so I'm not sure where that data came from, but in my own experience it's inaccurate. Yes, a hand-rolled integer conversion would be faster in most cases, so you are doing less (it's less general), but premature optimization is not good. Use hand-rolled where you need speed, otherwise don't re-invent the wheel.

At no point should stringstream, atoi, etc. have anything to do with it.

Quoted for truth - look at Cornstalk's post. The only place int to string (not vise-versa) is needed, is for putting the results in a new string if the OP desires.

I'd do it something like this:
Cast string to lowercase. (std::transform, and std::tolower)
Iterate over each char in string (just index into it)
I'd cast the char to an int
//Since ASCII lowercase letters begins at 97, and goes to 122, and space is 32...
if the int is 32
	 output a '-' to the result, if desired, or leave it as a space, or discard it.
if the int is over 122 or under 97
     output a ? to the result, or discard that char
otherwise
     Subtract 96, so 'a' becomes '1', and so on.

PARTNERS