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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:12:36+00:00 2026-05-19T03:12:36+00:00

Interview question: Which one will execute faster, if (flag==0) or if (0==flag) ? Why?

  • 0

Interview question: Which one will execute faster, if (flag==0) or if (0==flag)? Why?

  • 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-19T03:12:36+00:00Added an answer on May 19, 2026 at 3:12 am

    I haven’t seen any correct answer yet (and there are already some) caveat: Nawaz did point out the user-defined trap. And I regret my hastily cast upvote on “stupidest question” because it seems that many did not get it right and it gives room for a nice discussion on compiler optimization 🙂

    The answer is:

    What is flag‘s type?

    In the case where flag actually is a user-defined type. Then it depends on which overload of operator== is selected. Of course it can seem stupid that they would not be symmetric, but it’s certainly allowed, and I have seen other abuses already.

    If flag is a built-in, then both should take the same speed.

    From the Wikipedia article on x86, I’d bet for a Jxx instruction for the if statement: perhaps a JNZ (Jump if Not Zero) or some equivalent.

    I’d doubt the compiler misses such an obvious optimization, even with optimizations turned off. This is the type of things for which Peephole Optimization is designed for.

    EDIT: Sprang up again, so let’s add some assembly (LLVM 2.7 IR)

    int regular(int c) {
      if (c == 0) { return 0; }
      return 1;
    }
    
    int yoda(int c) {
      if (0 == c) { return 0; }
      return 1;
    }
    
    define i32 @regular(i32 %c) nounwind readnone {
    entry:
      %not. = icmp ne i32 %c, 0                       ; <i1> [#uses=1]
      %.0 = zext i1 %not. to i32                      ; <i32> [#uses=1]
      ret i32 %.0
    }
    
    define i32 @yoda(i32 %c) nounwind readnone {
    entry:
      %not. = icmp ne i32 %c, 0                       ; <i1> [#uses=1]
      %.0 = zext i1 %not. to i32                      ; <i32> [#uses=1]
      ret i32 %.0
    }
    

    Even if one does not know how to read the IR, I think it is self explanatory.

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

Sidebar

Related Questions

I was puzzled with one of the question in Microsoft interview which is as
This is one of an interview question which I had recently. I would like
This is an interview question. Referring to the sample code, which one of the
This was one of my interview questions which i couldn't answer.... Is it related
This is an interview question. I have K machines each of which is connected
Yesterday i had a question in an interview which i thought i could find
Here is one of my interview question. Given an array of N elements and
I have read Answers to C++ interview questions among which there is one that
I came across this post , which reports the following interview question: Given two
Another interview question which was expecting a true / false answer and I wasn't

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.