开发者

Using enums instead of strings speedup?

I have game and I mark the states with names. On each update working with states means, you work with their names, strings. I was wondering if it is worth it to replace the strings with enum开发者_开发百科 values?

Maybe it sounds dumb, but there are a lot of states and each one is processed at least 30 times per second.

Thanks


Well, are you having performance issues?

Certainly using integer values will be faster, but if as implemented it's fast enough, spend time elsewhere. Maybe leave yourself a todo in the comment.


Personally I think for maximum and probably unnecessary efficiency you should consider bit flags. These allow you to do the standard bitwise operators.

[Flags]
enum Choices
{
    OptionOne = 0x0,
    OptionTwo = 0x1    
}

class MyClass
{
    Choices mychoice = Choices.OptionOne | Choices.OptionTwo;
}


Enums are certainly cleaner in this instance and provide better maintainability... you're not going to typo a status that way (or you might, but the compiler will tell you). It's also going to be faster, doing a simple integer comparison over a string comparison.

That being said, if the coding is already done, and you're not having performance problems specific to this implementation of status, there's not much of a reason to change it. As others have said, profile it if that's your only issue.


By default, enums are internally treated as ints, so if you are checking many times then it is faster. However, if its only being checked 30 times a second, this cost is meaningless compared to other probable costs. Have you profiled to see what is slowest?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜