开发者

Most efficient way of responding to data stream in an iphone game

I'm making a game that uses nsstream to communicate with a server, and the program responds to the type of command coming for the stream (say the opponent jumps or shoots). If I have say 20 different types of commands, would: 1) 20 if statements (doubtful) 2) a switch - or 3) an NSDictionary of selectors corresponding to keys of the command types

be the fastest way to respond to the stream data?

for 2) would I say pass in integers as the command type and convert the data into intValue and pass into the switch?

and for 3) would this be any faster than a switch, or would the loo开发者_StackOverflow社区kup time and conversion make it not as fast?


I've been into gaming stream for quite much time. Among what you have suggested, I believe the

"2) would I say pass in integers as the command type and convert the data into intValue and pass into the switch"

will be the fastest one to process.


Generally speaking, a switch would give you the better performance among those options. In most cases, a C-family compiler is able to build a "jump table" from a switch statement, which lets it proceed directly to the correct case without even doing conditional evaluations.

But be careful about optimizing too early. A switch isn't necessarily the most readable, extensible, or maintainable way to implement what you want to do -- even if it's likely to be the best performing. This particular sequence might not even be a bottleneck for performance in your app. You may want to take al these other considerations when choosing your implementation.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜