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 7542431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:09:52+00:00 2026-05-30T08:09:52+00:00

I have this question in mind regarding optimazation of compiler when it comes to

  • 0

I have this question in mind regarding optimazation of compiler when it comes to shorthanded if/else.

I have this function:

double eu_distance (const coor& x, const coor& y) {

return ((y.x - x.x)*(y.x - x.x) + (y.y - x.y)*(y.y - x.y));
}

I am wondering what is more efficient?

min = min > eucl_distance(point_a, point_b) ? eucl_distance(point_a, point_b) : min;

or

double dis = eucl_distance(point_a, point_b);
if (min > dis)
    min = dis;

in the former case, does compiler (in my case, GCC 4.6.2) know how to optimize that if/else to keep the return value of eucl_distance() to reuse instead of computing it twice?

A piggy back question would be:

What is more efficient?

(y.x - x.x)*(y.x - x.x)

or

pow((y.x - x.x),2)

PS: Sorry that I cannot pick more than one correct answers!! 🙁 Thank you all for your answers! I really appreciate them!!

  • 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-30T08:09:53+00:00Added an answer on May 30, 2026 at 8:09 am

    There’s no universal answer: you’ll have to profile the code generated
    by your implementation to know for sure. In most cases, however, if
    eu_distance is in a separate translation unit, and is not specially
    annotated, the compiler will be unable to know that calling it twice
    with the same arguments will give the same results; in this case, the
    second form will almost surely be faster. On the other hand, If
    eu_distance can be inlined, any decent optimizer will end up
    generating almost exactly the same code for both.

    In practice, I would almost certainly use a third form:

    min = std::min( eu_distance( point_a, point_b ), min );
    

    (I am supposing that eucl_distance is a typo for eu_distance.)

    Also, I’d avoid a name like min. Somebody’s too likely to add a
    using namespace std; later, or even to include <windows.h>, without
    hvaing defined NOMINMAX. (<windows.h> defines min and max as
    macros if NOMINMAX has not been defined. Which leads to some
    interesting error messages if you define your own min or max. Or
    even include <algorithm>.)

    Concerning pow( x, 2 ): again, you’ll really have to measure, but
    typically, x * x will be faster, even if x is a complicated
    expression. (Of course, if the expression is non trivial, then
    recognizing that both x are identical may not be that easy, which
    makes the code harder to read. In such cases, you might want to
    consider a small function, say squared, which does nothing but return
    x * x. Inline it if it makes a difference in performance.)

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

Sidebar

Related Questions

I am new to db4o . I have this question in mind: when the
I have this little question that's been on my mind for a while now.
this is a noobie question regarding tree maps. I have read through the Java
As usually reading through stuff I got this question in mind. If I have
I have this question: Does implementing a custom MembershipProvider class needs you to implement
I have this question in which I have a SQL Server Compact Edition database
I have this question because I am not familiar latest generation of MS VS
I have this question: How could I call a codebehind method from jquery? I
I have this question relating to Lucene. I have a form and I get
I have this question, i am just throwing it out there. I am implementing

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.