Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6668825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:04:58+00:00 2026-05-26T03:04:58+00:00

I just learned that the compiler cleverly replaces calls to functions and variables by

  • 0

I just learned that the compiler cleverly replaces calls to functions and variables by essentially replacing them with the code they represent. With this in mind would the second method in fact be better below (due to clarity) and in fact run just as fast as the first one?

//check to see if sphere intersects the box
bool BoundingBox::Intersects(BoundingSphere boundingSphere)
{
// check for intersection on each axis by seeing if the radius is large enough to reach the edge of the cube on the 
// appropriate side. All must evaluate to true for there to be an intersection.
return (
    ((boundingSphere.Centre().x < negCorner.x && boundingSphere.Radius() > posCorner.x - boundingSphere.Centre().x) ||
     (boundingSphere.Centre().x > posCorner.x && boundingSphere.Radius() > boundingSphere.Centre().x - negCorner.x)) 
     &&
    ((boundingSphere.Centre().y < negCorner.y && boundingSphere.Radius() > posCorner.y - boundingSphere.Centre().y) ||
     (boundingSphere.Centre().y > posCorner.y && boundingSphere.Radius() > boundingSphere.Centre().y - negCorner.y)) 
     &&
    ((boundingSphere.Centre().z < negCorner.z && boundingSphere.Radius() > posCorner.z - boundingSphere.Centre().z) ||
     (boundingSphere.Centre().z > posCorner.z && boundingSphere.Radius() > boundingSphere.Centre().z - negCorner.z)));
}

Second Method:

//check to see if sphere intersects the box
bool BoundingBox::Intersects(BoundingSphere boundingSphere)
{
bool xIntersects, yIntersect, zIntersects;

xIntersects = 
    ((boundingSphere.Centre().x < negCorner.x && boundingSphere.Radius() > posCorner.x - boundingSphere.Centre().x) ||
     (boundingSphere.Centre().x > posCorner.x && boundingSphere.Radius() > boundingSphere.Centre().x - negCorner.x)));

yIntersects =   
            ((boundingSphere.Centre().y < negCorner.y && boundingSphere.Radius() > posCorner.y - boundingSphere.Centre().y) ||
     (boundingSphere.Centre().y > posCorner.y && boundingSphere.Radius() > boundingSphere.Centre().y - negCorner.y)));

zIntersects = 
            ((boundingSphere.Centre().z < negCorner.z && boundingSphere.Radius() > posCorner.z - boundingSphere.Centre().z) ||
     (boundingSphere.Centre().z > posCorner.z && boundingSphere.Radius() > boundingSphere.Centre().z - negCorner.z)));

return (xIntersects && yIntersects && zIntersects);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:04:58+00:00Added an answer on May 26, 2026 at 3:04 am

    First of all, this is not an accurate sphere vs. box check. It’s essentially a broken (it won’t report an intersection when the sphere center is contained in the box!) box vs. box-around-a-sphere test . Look for Arvo’s algorithm if you want to do this right.

    But back to your question: if there’s measurable speed difference, and I doubt there is, it most certainly wouldn’t be related to as much to inlining, but to the slightly different semantics of the two functions. The first function does lazy evaluation on its top-level via the && operator. So if you get a negative result on the first axis, it’ll bump out and not test the other ones. This will likely give you a speed advantage on a very slow computer when you have enough negative results on that axis or the second.

    The second function will not check whether you got a negative answer on a prior axis. As a result, it’ll always test all three. Now this will probably be faster on a decent computer, since it doesn’t have to wait for the results of the first checks to figure out whether it may execute the checks that are coming next. So this has less branch-mispredictions in general, which is often faster than just doing the work of both branches.

    Then again, the optimizer might me smart enough to figure out that it can execute the expressions on the other side of the && operator without side-effects. Inlining (either explicitly or via link-time code-generation) actually plays a small role here – as the optimizer needs to look at what the Centre and Radius functions actually do.

    But the only sure way to know is look at the generated code and benchmark it!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I just learned via a compiler error that in-class initialization of arrays is
I just learned that there are two types(?) of mySQL functions--mysql and mysqli. On
I am a newbie and just learned that if I define say package my.first.group.here;
I'm new to shell,I just learned that use (command) will create a new subshell
I am new to Box2d, and I have just learned that it does not
I just learned the hard way that IntPtr.Zero cannot be compared to default(IntPtr). Can
I just learned (the hard way) that Java Component s can only have one
I just learned of vim's gf command for navigating to files that are referenced
I just learned about ThreadLocal this morning. I read that it should always be
Having just learned that many cpp features (including the stl vector class) do not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.