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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:40:58+00:00 2026-06-03T01:40:58+00:00

i have technical wondering here guys , switch statement performs faster ,that thing i

  • 0

i have technical wondering here guys , switch statement performs faster ,that thing i know it

but what i want to know is how does it perform faster than if & else if ?

how can it find the controlExpression suitable case among all its cases directly?

and if i supposed that it is written using if else if it self to run and find the suitable case ,so it shouldnt perform faster ,it would perform the same as if else if?

so can u please answer me ? thanks in advance

  • 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-03T01:40:59+00:00Added an answer on June 3, 2026 at 1:40 am

    A switch statement basically executes the same sort of comparison for every case: var == a, var == b, var == c, etc.

    This page has details of how that’s translated into assembly by a compiler, but there are essentially three “kinds” of switch statements:

    1. switch statements with contiguous case integers – such as case 3: ... case 4: ... case 5: .... In these cases, the compiler can create a jump table — a listing of addresses to jump to in a contiguous block of memory and just calculate the offset, find the address, and jump. This can be faster than if-else if type chains. (Slightly slow if there’s only one case, of course.)
    2. switch statements with seemingly random case integers – such as case 12: ... case 106: ... case 9: .... In these cases, the compiler will just build an if-else if chain, so it can’t be faster than the if-else if type of code.
    3. switch statement with LOTS of seemingly random case integers – If there are a significant number, some compilers will build a binary search tree for all of the cases, so you have O(log(n)) time to execute any particular branch, which should improve the performance of your code. (Significant depends on the architecture you’re compiling on, since there’s extra overhead with checking which branch of the tree you should follow or if you should now jump.)

    This is a situation where you could outsmart the compiler, sometimes: If you know your cases can only be matched by some equation, like 3x+5, then you could build an array of function pointers, calculate the index ((caseNum - 5) / 3), and then execute it Continuation-Passing Style (or if you want to drive people batty, do the same calculation and build an array of goto labels, and then jump spaghetti-style. Either way you’d get the optimal “contiguous case”-style assembly with O(1) branching time.

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

Sidebar

Related Questions

For technical reasons on a site we may have two or more links that
Not very technical, but... I have to implement a bad words filter in a
I'm wondering about standards or commonalities for various common elements on webpages that have
I have multiple content types, but they all share some similarities. I'm wondering when
I have a technical interview on Monday and they were kind enough to give
I have a technical problem with using percentage in StringFormat method. The result of
I have a script which will be run interactively by non-technical users. The script
I have a series of ex-PDF documents (scientific/technical) with characters encoded as vector graphics
What are the technical questions I simply must have answers for before I approach
Sorry for a badly-phrased question. I have a requirement coming from a non-technical person

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.