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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:52:15+00:00 2026-05-22T19:52:15+00:00

I have this block in my program: if (x > 0) { a =

  • 0

I have this block in my program:

if (x > 0) {
    a = 1;
    b = 4;
} else {
    a = 2;
    b = 3;
}

This can be written with ternary operation like this:

a = (x > 0) ? 1 : 2;
b = (x > 0) ? 4 : 3;

The results are equivalents, but it’s part of a really critical part of my application, running thousands of times per second. I want to squeeze some microseconds, because this method will grow a little.

My question: in ARM level, which one is faster? I believe the first one creates a branch instruction. But what about the ternary operation? Does it become a branch on iPhone, too? Or iPhone’s ARM has a evil opcode to do the job?

BTW, I also saw an evil technique like this:

a = (x > 0) * 1 + (x <= 0) * 2;

Is this really faster?

  • 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-22T19:52:16+00:00Added an answer on May 22, 2026 at 7:52 pm

    EDIT:

    Just compiled your example, using GCC/LLVM, different optimizations, and looking at ARM6 and ARM7 assembly, here are my conclusions:

    • ARM-ASM differs depending on GCC / LLVM and target architecture
    • But, when using highest optimization level, it produce exactly the same assembly code for if and ternary, and that, whatever compiler/arch are. (yes, compared several pairs 😉

    Here is the most concise result with LLVM / ARM7, using the IT instruction you mentionned, for if and ternary:

    MOVS    R1, #2
    CMP     R0, #0
    IT GT
    MOVGT   R1, #1
    MOV.W   R2, #3
    IT GT
    MOVGT   R2, #4
    

    ENDOFEDIT

    Just searched a bit on the topic and even if some people thinks ternary is less optimized the most results and more relevant says that it produce the same assembly code.

    Take care that it might change with :

    1. compiler GCC, LLVM…
    2. optimization level

    I’m a bit lazy right now to disassemble code, but maybe I’ll edit that answer later.

    So I would think that djna is right, appart the the 2* (x>0), which would be really surprising if not optimized, this is the same.

    After that, ternary or not, it is a matter of taste.
    I prefer ternary when it makes sense in code and is readable.

    About second example it is a trick that use the fact that true == 1 / false == 0…
    Funny, but I wouldn’t like to maintain that code.

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

Sidebar

Related Questions

i have this block of xslt if-else case and was wondering if there's a
I have this line in a javascript block in a page: res = foo('<%=
Anyone have any experience using the Microsoft Configuration Management Application Block? This is an
I have this setup where in my development copy I can commit changes on
I have written a program that is sort of an unofficial, standalone plugin for
It seems I tend to attract strange issues. This time, I have written a
I have a WPF program built in C#, and part of it's function is
I have a FindFile routine in my program which will list files, but if
I have been developing a parental control software program for my company, but have
I have this code in jQuery, that I want to reimplement with the prototype

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.