Jump to content
  • Advertisement
Sign in to follow this  
formalproof

How much faster are enums compared to strings?

This topic is 3071 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

How much faster are enums compared to strings? Comparing two enums requires only one comparison operation, where as comparing two strings of length n will require anything between 1 and n operations (each character pair must be compared in the worst case.) Does anyone have practical experience about this question, especially in game programming? Have you come across a situation where using strings instead of enums was clearly too slow for your purposes?

Share this post


Link to post
Share on other sites
Advertisement
If you try to use very short strings and lookup them in a tree structure, they won't be slow. So it depends on how you use them.

Share this post


Link to post
Share on other sites
Quote:
Original post by the_edd
Quote:
Original post by formalproof
How much faster are enums compared to strings?

Ask your profiler.


I asked for general experience of other game programmers.

Share this post


Link to post
Share on other sites
Quote:
Original post by formalproof
Quote:
Original post by the_edd
Quote:
Original post by formalproof
How much faster are enums compared to strings?

Ask your profiler.


I asked for general experience of other game programmers.
IMO, 'ask your profiler' is a good answer to the question. There are so many variables involved that it's unlikely, I think, that someone else's experience with strings vs. enums would have much (if any) bearing on your particular situation. (That's just my take on it though - I could be wrong.)

In my own personal experience though, string operations (e.g. comparison) have never shown up as a hot spot (I don't recall ever noticing any string-related functions when profiling my applications). Naturally I'm not doing string comparisons in tight inner loops, but I do use them as keys for identifying resources, entities, and so on.

Is there a particular usage scenario that you have in mind? Also, what programming language(s) are you using?

Share this post


Link to post
Share on other sites
Quote:
Original post by formalproof
I asked for general experience of other game programmers.


You said it yourself: enum compare is O(1), string compare is O(n), so enum compares is always faster.

No experience needed!

Edit: How much faster exactly depends on n.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
Original post by formalproof
Quote:
Original post by the_edd
Quote:
Original post by formalproof
How much faster are enums compared to strings?

Ask your profiler.


I asked for general experience of other game programmers.
IMO, 'ask your profiler' is a good answer to the question. There are so many variables involved that it's unlikely, I think, that someone else's experience with strings vs. enums would have much (if any) bearing on your particular situation. (That's just my take on it though - I could be wrong.)

In my own personal experience though, string operations (e.g. comparison) have never shown up as a hot spot (I don't recall ever noticing any string-related functions when profiling my applications). Naturally I'm not doing string comparisons in tight inner loops, but I do use them as keys for identifying resources, entities, and so on.

Is there a particular usage scenario that you have in mind? Also, what programming language(s) are you using?


No, I don't have a particular scenario in mind. I'm just wondering if we can safely formulate a general rule that strings can be always preferred since the difference in speed is usually neglicible. Similar rules exist for questions such as "should assembly language be used or not?" ("no, unless you really have to"). Has anyone ever encountered a situation where strings are too slow compared to enums?

Share this post


Link to post
Share on other sites
Quote:
Original post by formalproof
No, I don't have a particular scenario in mind. I'm just wondering if we can safely formulate a general rule that strings can be always preferred since the difference in speed is usually neglicible. Similar rules exist for questions such as "should assembly language be used or not?" ("no, unless you really have to"). Has anyone ever encountered a situation where strings are too slow compared to enums?


I always use enums where possible. It's much faster and has the added benefit that typos are caught by the compiler and sensible suggestions are made by code completion.

Share this post


Link to post
Share on other sites
Quote:
Original post by Rattenhirn
Quote:
Original post by formalproof
I asked for general experience of other game programmers.


You said it yourself: enum compare is O(1), string compare is O(n), so enum compares is always faster.
Not necessarily true, if all of the strings differ in the very first character (or in the first couple), then a string comparison will also be O(1). That is, "abc" == "cde" will return false after a single comparison.

That's why you need to ask your profiler.

I do agree that enums are good because they help with intellisense (for example), but I don't think you should choose them because of "performance".

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!