How much faster are enums compared to strings?

Started by
45 comments, last by MaulingMonkey 14 years, 1 month ago
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?
Advertisement
What is the problem?
Where is it you are using strings where you get a performance loss ?
Quote:Original post by formalproof
How much faster are enums compared to strings?

Ask your profiler.
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.
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.
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?
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.
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?
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.
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".

This topic is closed to new replies.

Advertisement