Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

qed

performance: const pointer vs reference

This topic is 6596 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

I was wondering if there is a performance difference between using a const pointer or a reference as an argument of a function. Does anybody know? (i know there are aesthetic and other reasons to use references but my question concerns only potential performance differences) e.g.: do_something(myclass * const) or do_something(myclass &)

Share this post


Link to post
Share on other sites
Advertisement
Nope, the compiler should produce the same code for both. But the reference is potentially faster, cos you can''t have a NULL reference so there''s no need to check before you start using it.

Share this post


Link to post
Share on other sites
So if I don''t check for NULL it''s *exactly* as fast? (I know a reference can *potentially* be faster than a non-const pointer)

Share this post


Link to post
Share on other sites
Yes.

You can (and should) always generate a source code listing to test any of these kinds of questions yourself.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!