Jump to content
  • Advertisement
Sign in to follow this  
HiMK

Write ToLower As Efficient, Portable As Possible

This topic is 1274 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello

 

I am challenging myself to try to attempt to write a function that is as efficient, portable and failsafe as possible. The function is very simple and just converts a string (or wstring) to lower case. Are there any improvements you think I could make and any errors I have done?

#ifdef UNICODE
typedef std::wstring tstring;
typedef std::wstring::npos tnpos;
#else
typedef std::string tstring;
//typedef std::string::npos tnpos;  // throws compiler error?!
#endif


tstring toLower(const tstring& str)
{
    // Alternative 1: Returns a copy of a local string so not that efficient.
	
    unsigned int len = str.length(); 	// use unsigned int so I only have 4 bytes as opposed to 16 for int
    tstring lower(len, 'a'); 		// ensure string is correct size to avoid dynamic resizing. Reserve at construction; performing 2 steps at once defintion and reserving - is faster right?
    // or use lower.reserve(len);
	
    for (unsigned int i=0; i<len; i++)      // iterate using indexes. Maybe iterators could be more fail safe/efficient/etc.?
	lower[i] = tolower(str[i]);     
		
    return lower;
}

void toLower(const tstring& inStr, tstring& outStr)
{
    // Alternative 2: Have user define string and pass by reference (this would be faster?)
	
    unsigned int len = inStr.length();	// use unsigned int so I only have 4 bytes as opposed to 16 for int. Store length in local variable because we look it up again in the below for loop
    outStr.resize(len); 			// ensure string is correct size to avoid dynamic resizing.
	
    for (unsigned int i=0; i<len; i++)      // iterate using indexes. Maybe iterators could be more fail safe/efficient/etc.?
	outStr[i] = tolower(inStr[i]);
}
Edited by HiMK

Share this post


Link to post
Share on other sites
Advertisement

Thanks for the advice. So regarding unicode strings what libraries do you guys use? Boost C++?

Share this post


Link to post
Share on other sites

Generally, I use the ICU library's C bindings. I didn't like C++ bindings that ICU provides, and by the time the boost::locale wrapper for ICU got introduced, I already had my own C++ wrappers that do what I want.

Share this post


Link to post
Share on other sites
Out of curiosity, where in your game did a profiler say tolower was a performance issue?

I agree with the above that it is best left to the standard library.

Another alternative is to lowercase your strings once and keep one always lower cased version for comparison and the original version for display.

It takes more ram but it is faster.

You could also.look at comparing hashes of lowercase strings instead (this can be even more efficient still).

Anyhow beware the premature optimisation monster!

Share this post


Link to post
Share on other sites


Out of curiosity, where in your game did a profiler say tolower was a performance issue?

To be fair to the OP, he never said he was planning to use this in a game.

 


I am challenging myself to try to attempt to write a function that is as efficient, portable and failsafe as possible.

 

I think it's more along the lines of a programming exercise.

 

Kinda like writing your own linked list; everyone should write one once and then never ever use it. :)

Share this post


Link to post
Share on other sites

"Portable" and "Efficient" is almost a polar opposite of each other.

 

Efficiency makes certain assumptions and/or optimizations based on current architecture/environment/encoding (e.g. ASCII only).

Portable means that it must be usable in as many architectures and environments as possible.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!