Skip to main content
gamedev.net
🔒 Locked

Some Std::string advice?

Started by maxpenguin 18 replies 3k views
Original Post
maxpenguin
maxpenguin
Hi All, Just a little background - I am currently working on a fairly simple cpp game/engine thingie (as I know lots of people are) to eventually use when applying for a game dev position. I have used Std::string several times in my little game. I am reading a book about game engines, and the first thing the author does in the book is go through and re-implement all of the std::* stuff - for higher performance. Now my question is - does this mean that std::strings are bad over const char*? I realise that there is some overhead involved with std::string and others but how bad is it? Like for example, would you cringe if you got my demo app's source code and I used a few std::strings? Also, is this sort of thing a bad thing to do?

		std::string message;
		message.append("Unable to create D3d Device! Dx Error: ");
		message.append(DXGetErrorString(hr));
		message.append(" -> ");
		message.append(DXGetErrorDescription(hr));

		throw message.c_str();

(I have a catch higher up) I would appreciate any advice and insight! Thanks.
njpaul
njpaul
I'd cringe if you didn't use std:string. The stl has pretty good performance. Maybe (somebody might know for sure) the stl used to have poor performance and your book is just old. Anyways, don't rewrite it unless you're sure it's a bottleneck in your code. Also, you shouldn't throw string literals.
jflanglois
jflanglois
Quote:
Original post by njpaul
I'd cringe if you didn't use std:string. The stl has pretty good performance. Maybe (somebody might know for sure) the stl used to have poor performance and your book is just old. Anyways, don't rewrite it unless you're sure it's a bottleneck in your code. Also, you shouldn't throw string literals.

Quoted for truth and emphasis.
maxpenguin
maxpenguin
Awsome, thanks for the help.

Also, appreciate the link on not throwing string literals. I actually saw it in a book, and thought it was a bit weird, but must be okay if they were doing it like that hehe [looksaround].

Thanks again [grin].
cpiasminc
cpiasminc
Quote:
I'd cringe if you didn't use std:string. The stl has pretty good performance. Maybe (somebody might know for sure) the stl used to have poor performance and your book is just old.

One of my previous jobs, there were some ideologies in place that really hurt, which meant that we often had to implement our own versions of various STL components if we had a use for them. For one, the owner was a staunch Microsoft-hater, so he wouldn't allow anything other than Xbox builds to go through Microsoft's compiler. Also, when it came to the PS2, we had what was probably the very first version of GCC available for it, and stuck with it on the basis of "it's worked for us before." So in the end, all the code we wrote had to be mindful of some 5 different C++ compilers.

The support for STL (templates in general, actually) in some of these was next to zero.
Light travels faster than sound. This is why many people appear very bright until they speak.
Spoonbender
Spoonbender
Quote:
Original post by maxpenguin
I have used Std::string several times in my little game. I am reading a book about game engines, and the first thing the author does in the book is go through and re-implement all of the std::* stuff - for higher performance.

That just proves one thing. The author is a moron. Even if std::string had lousy performance, a beginners book is *not* the place to "fix" it.
And when *starting out* with an engine, it's also not the place to "fix" it.

Even if std::string was slow, it should be fixed *when it's clear its performance is unacceptable*. That means, when you have an engine up and running, and can profile the code and see what part takes up the most CPU time.

There's no reason why a std::string would be slower. It has the potential to be faster in some cases, because it's not as hung up on the idea that strings must be zero-terminated and have no other assiciated data.
maxpenguin
maxpenguin
Man, working with 5 different compilers must have been a NIGHTMARE! I recently wrote some code (in my day job) that had to run on Windows XP and Windows Mobile (pocket pc) and I thought THAT was bad [wow]!

Have you filled your game programmer positions cpiasminc? Are you interested in a beginner c++ programmer that is a very fast learner and is keen to relocate to the usa? [wink][lol].

Thanks Again!


Edit:Hrm, that was 2003 - nevermind [imwithstupid].
Deyja
Deyja
Quote:
That just proves one thing. The author is a moron. Even if std::string had lousy performance, a beginners book is *not* the place to "fix" it.
And when *starting out* with an engine, it's also not the place to "fix" it.


However, implementing std::string can be a great learning experience. I teach programming to high schoolers (Though I can't teach anything else until I get a liscense, heh) and the first four weeks of programming II (which uses C++) are spent implementing a string class (programming I uses C). Then in the fifth week, I make them delete it, and use std::string instead. :)
cpiasminc
cpiasminc
Quote:
Original post by maxpenguin
Man, working with 5 different compilers must have been a NIGHTMARE! I recently wrote some code (in my day job) that had to run on Windows XP and Windows Mobile (pocket pc) and I thought THAT was bad [wow]!

Have you filled your game programmer positions cpiasminc? Are you interested in a beginner c++ programmer that is a very fast learner and is keen to relocate to the usa? [wink][lol].

Edit:Hrm, that was 2003 - nevermind [imwithstupid].

Actually, that was about a year ago (2005)... but that company fell on some hard times due to their complete and utter dependence on a single 2nd/3rd-tier publisher for all their projects.

I've since moved on, as has almost everybody I worked with over there save for a handful of people. Certainly, their turnover rate for programmers is nowhere near as bad as their turnover rate for artists (what do you expect when so many artists get paid less than $20k and have no leads?)

I believe those 5 compilers has been reduced to 4 at least now since funding for a next-gen title is very unlikely for them in the near future, which takes away 1 platform (X360) since back then.
Light travels faster than sound. This is why many people appear very bright until they speak.
maxpenguin
maxpenguin
Deyja:
Sounds like it would be interesting and tricky to implement your own std::string like class. It is definatly on my list of things to do [grin]. I wish they taught c++ at *my* high school (we only had VB 6 [sick]!)

cpiasminc:
Well, atleast there is one less compiler for them to worry about I guess. So, if you don't mind me asking, how did you get into the game industry? Do you know what the demand for entry game programmers is in (is it Texas still)? Any tips on getting a game programming job?

A bit off topic for the std::string advice thread, but oh well. I am a c# .net "business application" programmer by day, and it is just draining the life out of me. So I want to learn everything I can to help me move on, hopefully, into the game industry.

[Edited by - maxpenguin on July 25, 2006 3:33:33 PM]
Deyja
Deyja
Um. I'm not in the game industry. In fact, I'd rather have your job.

Or you could specify that you are talking to the other guy, after you already started the post with me!
maxpenguin
maxpenguin
Yeah sorry, my mistake. I have edited it to hopefully make a little more sense.
grhodes_at_work
grhodes_at_work
I'll throw in my vote that you should use STL rather than write your own. But, you should be careful to use it well. STL performance can be quite good, and hard to match, but if you use it poorly it can become a bottleneck.

I would recommend that you consider buying an MP3 of Pete Isensee's talk from GDC 2004 (its a US$6 download, link below). Great lecture. It is quite enlightening about C++ performance issues. Getting performance right, using STL or your own code, will be something that impresses game companies!

Common C++ Performance Mistakes in Games

Pete also did a followup talk in 2006, focused on multicore type architectures. Hmmm....showing expertise on multicore would come in handy also!
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
maxpenguin
maxpenguin
Awesome, thanks for the advice and link!

Sounds like its well worth $6, so I will defiantly get a few. The multi core/multi threaded stuff would really be useful too.
etothex
etothex
Am I the only one uncomfortable with the throwing of a .c_str()? Or is it just me?
maxpenguin
maxpenguin
Yeah, After reading a little more into throwing string literals I'm uncomfortable throwing .c_str's [looksaround].
d000hg
d000hg
Working to improve your understanding of the STL is greatly beneficial. string is incredibly useful.

If anyone's interested I made a class which adds some of the java string functionality - any comments appreciated...
template<class E> class jbasic_string : public basic_string<E>{public:jbasic_string(){}jbasic_string(const E *s) : basic_string<E>(s){}jbasic_string(const E *s, int n) : basic_string<E>(s,n){}jbasic_string(const basic_string<E> &s) : basic_string<E>(s){}jbasic_string(const basic_string<E> &s, int p, int n) : basic_string<E>(s,p,n){}jbasic_string(int n, E c) : basic_string<E>(n,c){}bool operator==(const jbasic_string &x) const { return compare(x)==0;}bool operator<(const jbasic_string &x) const { return compare(x)<0;}bool operator>(const jbasic_string &x) const { return compare(x)>0;}jbasic_string left(int l) const{return basic_string<E>::subasic_stringtr(0,l); }jbasic_string right(int l) const{ return subasic_stringtr(l>basic_string<E>::length()?0:basic_string<E>::length()-l); }bool ends_with(const jbasic_string &s) const {return s==right(s.basic_string<E>::length());}bool starts_with(const jbasic_string &p) const {return p==left(p.basic_string<E>::length());}};typedef jbasic_string<char> jstring;typedef jbasic_string<wchar_t> jwstring;
Zahlman
Zahlman
Quote:
Original post by d000hg
Working to improve your understanding of the STL is greatly beneficial.


I agree. Which is why you should understand that (a) the standard library containers are not designed for inheritance; (b) the std::string class already contains too much stuff as it is (lots of member functions could - and should, according to the arguments of the C++ gurus - be redone as free functions making use of a base set); (c) the 'jstring' effort is therefore misguided.

Cutting out the stuff that reimplements things that are already there, and fixing some weird search-and-replace errors, we get:

jbasic_string left(int l) const {  return basic_string<E>::substr(0,l); }jbasic_string right(int l) const {   return substr(l > basic_string<E>::length() ?                 0 :                 basic_string<E>::length() - l); }bool ends_with(const jbasic_string &s) const {  return s == right(s.basic_string<E>::length());}bool starts_with(const jbasic_string &p) const {  return p == left(p.basic_string<E>::length());}


We can implement this sort of stuff much more easily as free functions - and actually, making use of some other standard library stuff:

template <typename CharT>std::basic_string<CharT> left(const std::basic_string<CharT>& s, int dist) {  return s.substr(0, dist); }std::basic_string<CharT> right(const std::basic_string<CharT>& s, int dist) {  return s.substr(s.length() - std::max(dist, s.length())); }bool ends_with(const std::basic_string<CharT>& str, const                std::basic_string<CharT>& suffix) {  int biglen = str.length();  int smalllen = suffix.length();  return (biglen >= smalllen) && str.compare(biglen - smalllen, smalllen, suffix) == 0; // could also implement using std::equal() and reverse_iterators}bool starts_with(const jbasic_string &p) const {  int biglen = str.length();  int smalllen = suffix.length();  return (biglen >= smalllen) && str.compare(0, smalllen, suffix) == 0; // could also implement using std::equal()}
cpiasminc
cpiasminc
Quote:
Well, atleast there is one less compiler for them to worry about I guess. So, if you don't mind me asking, how did you get into the game industry? Do you know what the demand for entry game programmers is in (is it Texas still)? Any tips on getting a game programming job?

Well, basically just started out going heavy on the graphics and 3d math and published a fair bit of research on it while in high school and college. Did the whole demoscene bit for a while (which I'd say was valuable from the perspective of making the connection between the mathematical and the aesthetic). Learning assembler and a lot of the basics of CPU architecture inside and out was my lifeblood, not so much because it was a useful language by itself but because it gives one a perspective on the underlying business going on inside the CPU. And while I'm fairly specialized, it was enough to form a basis for everything else in game development.

The demand for entry-level... I don't know how high it is these days, but you can still find plenty of not-so-entry-level jobs that don't require much experience and only require you to be able to handle tests of a similar level of difficulty. The main thing isn't so much to know how to do everything so much as to know enough that you can derive solutions (which is more important in the long run, anyway). I'm certainly not in Texas anymore, but I've found that throughout the industry, you typically have to find yourself in sh*tholes for a while before moving to places that are reputable.

I had to go from working for a guy who shot himself to a guy who idolized Adolf Hitler to a guy who lived and breathed anti-MS dogma to my current job, where I get to recreate a major flagship character (which shall go unnamed for now) for next-gen consoles.
Light travels faster than sound. This is why many people appear very bright until they speak.
maxpenguin
maxpenguin
Thanks for your insight, I really appreciate it [grin].

I always wanted to learn assembly, the inner workings of the proccessor and all of that has always facinated me. So my assembly books are on the way from Amazon (damn, living in a small country in the relitive middle of nowhere with overly expensive slow shipping [tears]. oh well) so I can't wait.

I am definatly working on skills and knowledge, so I can tackle the not-so-entry-level problems when that day comes.


Thanks again!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.