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

  • Home
  • SEARCH
  • 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 7508025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:36:11+00:00 2026-05-29T22:36:11+00:00

I have the following code that I use to compute the distance between two

  • 0

I have the following code that I use to compute the distance between two vectors:

double dist(vector<double> & vecA, vector<double> & vecB){
    double curDist = 0.0;
    for (size_t i = 0; i < vecA.size(); i++){
        double dif = vecA[i] - vecB[i];
        curDist += dif * dif;
    }

    return curDist;
}

This function is a major bottleneck in my application since it relies on a lot of distance calculations, consuming more than 60% of CPU time on a typical input. Additionally, the following line:

double dif = vecA[i] - vecB[i];

is responsible for more than 77% of CPU time in this function. My question is: is it possible to somehow optimize this function?

Notes:

  • To profile my application I have used Intel Amplifier XE;
  • Reducing the number of distance computations is not a feasible solution for
    me;
  • 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-29T22:36:12+00:00Added an answer on May 29, 2026 at 10:36 pm

    There are two possible issues I can think of right now:

    • This computation is memory bound.
    • There is an iteration-to-iteration dependency on curDist.

    This computation is memory bound.

    Your dataset is larger than your CPU cache. So in this case, no amount of optimization is going to help unless you can restructure your algorithm.


    There is an iteration-to-iteration dependency on curDist.

    You have a dependency on curDist. This will block vectorization by the compiler. (Also, don’t always trust the profiler numbers to the line. They can be inaccurate especially after compiler optimizations.)

    Normally, the compiler vectorizer can split up the curDist into multiple partial sums to and unroll/vectorize the loop. But it can’t do that under strict-floating-point behavior. You can try relaxing your floating-point mode if you haven’t already. Or you can split the sum and unroll it yourself.

    For example, this kind of optimization is something the compiler can do with integers, but not necessarily with floating-point:

    double curDist0 = 0.0;
    double curDist1 = 0.0;
    double curDist2 = 0.0;
    double curDist3 = 0.0;
    for (size_t i = 0; i < vecA.size() - 3; i += 4){
        double dif0 = vecA[i + 0] - vecB[i + 0];
        double dif1 = vecA[i + 1] - vecB[i + 1];
        double dif2 = vecA[i + 2] - vecB[i + 2];
        double dif3 = vecA[i + 3] - vecB[i + 3];
        curDist0 += dif0 * dif0;
        curDist1 += dif1 * dif1;
        curDist2 += dif2 * dif2;
        curDist3 += dif3 * dif3;
    }
    
    //  Do some sort of cleanup in case (vecA.size() % 4 != 0)
    
    double curDist = curDist0 + curDist1 + curDist2 + curDist3;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code snippet that i use to compile class at the run
I have several blocks of the following code that each use there own matrix.
I have the following snippet of code that's generating the Use new keyword if
I have the following code that creates two objects (ProfileManager and EmployerManager) where the
I have following code that does not work due to a being a value
I have the following code that shows either a bug or a misunderstanding on
I have the following code that won't compile and although there is a way
I have the following code that sets a cookie: string locale = ((DropDownList)this.LoginUser.FindControl(locale)).SelectedValue; HttpCookie
I have the following code that I need to add an additonal object to
I have the following code that creates a serverside object of the xmlhttp class.

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.