Jump to content
  • Advertisement
Sign in to follow this  
gsamour

performance of comparing pointers with less than operator

This topic is 3048 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is comparing pointers with "<" or ">" operators slow? Is it the same speed as comparing an integer? I'm working on a 32-bit machine. Would this be different on a 64-bit machine?

Share this post


Link to post
Share on other sites
Advertisement
The time it takes to compare pointers is of the same order of the time it takes to compare integers, which is several orders of magnitude smaller than the time it takes to load a page of memory that is not cached, for example.

My point is that you should probably look for opportunities for optimization elsewhere (better algorithms, cache coherency, avoid branch misprediction...).

Share this post


Link to post
Share on other sites
I'm not really trying to optimize. I just remember one time I was using a std::map with a custom key. The "<" operator on the key struct/class used a pointer to compare... then it was changed to compare using an int. I think we changed it because it was slower with the pointer. But it seems I'm mistaken, so I'll try the experiment again. Thanks for the reply.

Share this post


Link to post
Share on other sites
The result of comparing pointers that don't point into the same object might be unspecified in the first place.

Quote:

--If two pointers p and q of the same type point to different objects
that are not members of the same object or elements of the same
array or to different functions, or if only one of them is null, the
results of p<q, p>q, p<=q, and p>=q are unspecified.


However, it should still be OK to use pointers as a key for a map (std::map<X*, Y>), because it should use the std::less<T> functor and

Quote:

8 For templates greater, less, greater_equal, and less_equal, the spe-
cializations for any pointer type yield a total order, even if the
built-in operators <, >, <=, >= do not.


But if you write your own comparison function, and compare unrelated pointers directly with relational operators, it might be a bit incorrect in the first place.

Not sure, though, may-be unspecified means here that there's no way of knowing if p < q or q < p, but as long as they define a consistent ordering, you should be OK?

Share this post


Link to post
Share on other sites
Quote:
Original post by visitor
Not sure, though, may-be unspecified means here that there's no way of knowing if p < q or q < p, but as long as they define a consistent ordering, you should be OK?
I'm going to say that 'unspecified results' here means that any ordering relation remains unspecified too. They haven't explicitly stated any ordering for the binary comparison operators on pointers so you probably shouldn't assume one. The functor templates, however, definitely define a total-order relation [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!