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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:26:26+00:00 2026-06-06T16:26:26+00:00

This is really just sort of an academic question, I’m just curious to know

  • 0

This is really just sort of an academic question, I’m just curious to know which one is faster. I’m guessing the difference is negligible but still, I’d like to know.

if( (x == 1) || (x == 2) )

vs

if (x < 3)

thanks!

  • 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-06T16:26:28+00:00Added an answer on June 6, 2026 at 4:26 pm

    In the form you provided there is evident difference in complexity: the first code uses 3 operators, then the second — just one. But OK, lets put this code of and assume you want to compare > (or <) and == (!=). If you have ever faced assembler while examing your programs (but i bet you didn’t) you would notice such code

    if (x < 3) /* action */;
    

    being translated to smth like (for x86 CPU):

      cmp %eax, 3   // <-- compare EAX and 3. It modifies flags register(*1) (namely ZF, CF, SF and so on)
      jge @@skip    // <-- this instruction tells CPU to jump when some conditions related to flags are met(*2).
                    // So this code is executed when jump is *not* made (when x is *not*
                    // greater or equal than 3.
      /* here is action body */
    @@skip:
      // ...
    

    Now consider this code:

    if (x == 3) /* action */;
    

    It will give almost the same assembly (of course, it may differ from mine, but not semantically):

      cmp %eax, 3
      jne @@skip // < -- here is the only difference
      /* action is run when x is equal to 3 */
    @@skip:
      ...
    

    Both of this operators (jge and jne and others jumps) do their job with the same speed (because CPUs are made so, but it obviously depends on its architecture). The more impact on performance does distance of jump (difference between code positions), cache misses (when processor wrongly predicted the jump), and so on. Some instructions are more effective even (use less bytes for example), but remember the only thing: do not pay them so much attention. It always better to make algorythmic optimizations, don’t do savings on matches. Let the compiler do it for you — it is really more competent in such questions. Focus on your algorythms, code readablity, program architecture, fault-tolerance.. Make speed the last factor.

    (*1): http://en.wikipedia.org/wiki/FLAGS_register_%28computing%29
    (*2): http://www.unixwiz.net/techtips/x86-jumps.html

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

Sidebar

Related Questions

This really isn't an important question. I just wanted to know which method is
This is really just a best practices question... I find that When developing an
I don't know if my mind just fools me or this is really not
This is not really a question because I just solved the problem, but I
This is really just for my own use: I would like to be able
This program reads emails (really just a .txt file structured like an email) and
This really shouldn't be hard, I just can't figure out how to do it.
I'm just learning MVC3 now and this is really confusing me. I have a
This seems really dumb. I've tried a bunch of different ways and it's just
I am trying to convert this to python. I just really need help with

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.