开发者

Most efficient way to compare two __int64 numbers and get -1,0,1

I have been struggling with a very simple problem... I am working with a 4 dimensional cube using AVL trees... now the problem is a performance related one... basically I have to compare billions of 64 bit numbers... (because my key is 64bit comprising the 4 dimensions of 16bits each)...

The problem is not that I can't compare 2 64-bit numbers, but rather I need to do it in as few as possible clock cycles.

Unfortunately my AVL Tree template has a signature of "int CompareKeyNode(key k, handle h)"

Under the hood I have two __int64 numbers lhs & rhs, unfortunately the contract of this method is: 1. lhs==rhs return 0 2. lhs > rhs return 1 3. lhs < rhs return -1

Now if the above method expected an __int64 number I could simply do a (METHOD A) return lhs - rhs;

Unfortunately it only needs a 32 bit integer, so I have to resort to something akin to ((METHOD B)) ie. return lhs == rhs ? 0 : (lhs < rhs ? -1 : 1)

The problem to me is that to load my data using (METHOD A) takes 60 secs whilst (METHOD B) takes 117 seconds.

Unfortunately (METHOD A) is incorrect as it loses precision on the return for casting the lhs-rhs statement.

The processing time is crucial to me and I am sure there must be a simple efficient answer which is eluding me right now...

Does anybody have an idea on how to solve this? Or perhaps any suggestions?

I am working in VC++ (un-managed) but surely .NET/Java must have solved this problem in there JITing/VM stage... otherwise they would be taking a huge performance hit.

PS: Have tried memcmp() but no开发者_JAVA技巧t good enough...


In assembly, subtract them.
zero flag set = 0
carry flag set = -1
else 1


You could try (lhs<rhs) - (rhs<lhs). No guarantee that it'll give an improvement (at all), but it's at least possible that it might...


Really all I can see is that you should either overload or rewrite the function to accept __int64. You're always gonna get a performance loss with inline logic like that. Why can't you overload your class to accept __int64?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜