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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:03:07+00:00 2026-06-16T13:03:07+00:00

Possible Duplicate: What is the relative performance difference of if/else versus switch statement in

  • 0

Possible Duplicate:
What is the relative performance difference of if/else versus switch statement in Java?

Given the following two methods:

public static int useSwitch(int i) {
    switch (i) {
    case 0:
        return 1;
    default:
        return 0;
    }
}

public static int useIf(int i) {
    if (i == 0)
        return 1;
    return 0;
}

testing shows that the switch executes marginally faster (1.4 nanoseconds per call on my machine) than the if version.

I had always believed that the benefit of a switch didn’t kick in until at least a few ifs could be avoided,

Why is switch faster than a single if?

  • 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-16T13:03:08+00:00Added an answer on June 16, 2026 at 1:03 pm

    By checking the bytecode the result is as expected:

    SWITCH

    public static useSwitch(I)I
     L0
      ILOAD 0
      TABLESWITCH
        0: L1
        default: L2
     L1
      INVOKESTATIC Tests.a()I
      IRETURN
     L2
      INVOKESTATIC Tests.b()I
      IRETURN
    

    IF

    public static useIf(I)I
     L0
      ILOAD 0
      IFNE L1
     L2
      INVOKESTATIC Tests.a()I
      IRETURN
     L1
      INVOKESTATIC Tests.b()I
      IRETURN
    

    Now I don’t see any particular reason for which one should be slower than the other (not by a significative amount in any case). This is surely something that is related to the specific JVM implementation and how it executes these opcodes. According to common knowledge the TABLESWITCH instruction should be slower unless there are enough cases that makes its construction valuable but this is just common thinking. Every JVM could implement it differently so this is just speculation.

    Are you sure to profiled everything in a consistent way? (by giving time to JVM to warm up, by keeping just results within a confidence range and all the other things that make profiling enough correct to be used)

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

Sidebar

Related Questions

Possible Duplicates: Is "else if" faster than "switch() case"? What is the relative performance
Possible Duplicate: How to construct a relative path in Java from two absolute paths
Possible Duplicates: is “else if” faster than “switch() case” ? What is the relative
Possible Duplicate: How to calculate the angle between two points relative to the horizontal
Possible Duplicate: Cyclic module dependencies and relative imports in Python Consider the following example
Possible Duplicates: is “else if” faster than “switch() case” ? What is the relative
Possible Duplicate: Difference between declaration and malloc Is there a difference between these two
Possible Duplicate: WPF Image to byte[] Relative to this I have a BitmapSource image
Possible Duplicate: How do I calculate relative time? I want to convert Date Value
Possible Duplicate: How do I calculate relative time? I am searching for a custom

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.