开发者

Large integer value causes performance hit in c++?

I'm working on a home made board game (computer board game of course). As part of the game I have a strength value for each unit on the board. One of these units is special and has practically unlimited power. I want to represent this with integer max value (the strength is an int var).

My problem is that this value is being compared a lot (a whole lot!), and so I found myself asking, is int max val any worse then a value of 100 (which also translate to unlimited when compared to other 开发者_高级运维units).

in other words, can this code:

this->_strength == other->_strength;

be affected (in regard to speed) by the value of _strength (under full compiler, OS and hardware specific optimisations).

P.S, This is more of a theoretical question on integer optimisations, then a practical one about my game, which is why I didn't bother with detail about it.


As long as you're sticking to max value, no. An integer comparison on most any hardware architecture you're likely to use will perform that comparison in bit-wise parallel on the whole word, and do it one instruction.

Now, should you happen to use a packed decimal representation like COMP-3 in COBOL, it could.


No. The only things that would play into the speed comparing two integers of the same type would be their size in memory compared to the word size of the processor (in other words, how the processor loaded the integers to do the comparison). The actual value of an integer will have absolutely no effect on the speed of the comparison.


Each int variable takes up the same amount of memory (typically 32 bits in a 32 bit machine). The actual value within it has really no effect on the number of bits allocated to it. Thus an int with a value of 0 has the same effect as an int with a value of 2^31-1.


Looks as a typical premature optimization problem, familiar to Performance of 32-bit integers in a 64-bit environment (C++)

And I am sure that you do not have any chance to hit any potential performance problem concerning low level operations in a project of such a small scale.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜