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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:30:07+00:00 2026-06-15T10:30:07+00:00

The following function compare two arrays, and returns true if all elements are equal

  • 0

The following function compare two arrays, and returns true if all elements are equal taking in account a tolerance.

// Equal
template<typename Type>
bool eq(const unsigned int n, const Type* x, const Type* y, const Type tolerance)
{
    bool ok = true;
    for(unsigned int i = 0; i < n; ++i) {
        if (std::abs(x[i]-y[i]) > std::abs(tolerance)) {
            ok = false;
            break;
        }
    }
    return ok;
}

Is there a way to beat the performances of this function ?

  • 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-06-15T10:30:08+00:00Added an answer on June 15, 2026 at 10:30 am

    Compute abs(tolerance) outside the loop.

    You might try unrolling the loop into a ‘major’ loop and a ‘minor’ loop where the ‘minor’ loop’s only jump is to its beginning and the ‘major’ loop has the ‘if’ and ‘break’ stuff. Do something like ok &= (x[i]-y[i] < abstol) & (y[i]-x[i] < abstol); in the minor loop to avoid branching — note & instead of &&.

    Then partially unroll and vectorise the minor loop. Then specialise for whatever floating-point types you’re actually using and use your platform’s SIMD instructions to do the minor loop.

    Think before doing this, of course, since it can increase code size and thereby have ill effects on maintainability and sometimes the performance of other parts of your system.

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

Sidebar

Related Questions

I'm attempting to compare two tables of equal length with a function, since I
I would like help finishing my function that compares two arrays and returns the
I'm attempting to write a function to compare two arrays of pointers to see
I wrote a template function to compare two variables: template <class t> int compare(const
Can I write a hash code function for the following comparer logic? Two instances
The following function rewrites urls from news and product titles that contain all sorts
I would like to use a match with function to compare two matrix. I
I am writing a function to compare the contents of two lists. The order
I have the following function which compares the first column value in two 2D
I'm generating two matrices using the following function (note some code is omitted): srand(2007);

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.