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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:30:51+00:00 2026-05-22T00:30:51+00:00

That is, what is the fastest way to do the test if( a >=

  • 0

That is, what is the fastest way to do the test

if( a >= ( b - c > 0 ? b - c : 0 ) &&
    a <= ( b + c < 255 ? b + c : 255 ) )

    ...

if a, b, and c are all unsigned char aka BYTE. I am trying to optimize an image scanning process to find a sub-image, and a comparison such as this is done about 3 million times per scan, so even minor optimizations could be helpful.

Not sure, but maybe some sort of bitwise operation? Maybe adding 1 to c and testing for less-than and greater-than without the or-equal-to part? I don’t know!

  • 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-22T00:30:51+00:00Added an answer on May 22, 2026 at 12:30 am

    Well, first of all let’s see what you are trying to check without all kinds of over/underflow checks:

    a >= b - c
    a <= b + c
    subtract b from both:
    a - b >= -c
    a - b <= c
    

    Now that is equal to

    abs(a - b) <= c
    

    And in code:

    (a>b ? a-b : b-a) <= c
    

    Now, this code is a tad faster and doesn’t contain (or need) complicated underflow/overflow checks.


    I’ve profiled mine and 6502’s code with 1000000000 repitions and there officially was no difference whatsoever. I would suggest to pick the most elegant solution (which is IMO mine, but opinions differ), since performance is not an argument.


    However, there was a notable difference between my and the asker’s code. This is the profiling code I used:

    #include <iostream>
    
    int main(int argc, char *argv[]) {  
        bool prevent_opti;
        for (int ai = 0; ai < 256; ++ai) {
            for (int bi = 0; bi < 256; ++bi) {
                for (int ci = 0; ci < 256; ++ci) {
                    unsigned char a = ai;
                    unsigned char b = bi;
                    unsigned char c = ci;
                    if ((a>b ? a-b : b-a) <= c) prevent_opti = true;
                }
            }
        }
    
        std::cout << prevent_opti << "\n";
    
        return 0;
    }
    

    With my if statement this took 120ms on average and the asker’s if statement took 135ms on average.

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

Sidebar

Related Questions

What's fastest way to re-test iPhone core data migration to a new version? That
I'm looking for the fastest way to check that entry exists... All my life,
What is the simplest, fastest way to complete the PHP code below such that
I'm wondering what is the fastest way that I can write some code. I
I am trying to figure out the fastest way (in PHP 5) to check
I'm trying to find the fastest way to get the complete preprocessed source code
What is the fastest way to implement a new class that inherits from List<T>
Since I use jQuery 1.3+ all except one timed test is using that. The
I have a tab-delimited file that has over 200 million lines. What's the fastest
It's my understanding that, in terms of selector speed, that #ID selectors are fastest,

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.