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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:54:31+00:00 2026-05-17T22:54:31+00:00

What can I do to improve large switches and if-elses speed manually? I will

  • 0

What can I do to improve large switches and if-elses speed manually? I will probably need some kind of hash or lookup table.

I’m working with gcc and C code, I doubt that gcc has any inbuilt optimizations for this.

Edit:
My switch code is what every switch looks like, do something based on if a particular int is some value.
My if-elses look like this:

if( !strcmp( "val1", str ) )
foo();
else if( !strcmp( "val2", str ) )
foo2();
...

I also have ifs that do this

if( struct.member1 != NULL )
foo();
if( struct.member2 != NULL )
foo2();

EDIT2:
Thank you everyone. I’m not sure which one I should pick as an answer, because a lot of these answers have valid points and valuable insights. Unfortunately, I have to pick just one. But thanks all!
In the end, using a perfect hash table seems the best way to get O(n) time on the access for both ifs and switches.

  • 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-17T22:54:31+00:00Added an answer on May 17, 2026 at 10:54 pm

    To use a hash table:

    1. Pick a hash function. This one is a biggie. There are tradeoffs between speed, the quality of the hash, and the size of the output. Encryption algorithms can make good hash functions. The hash function performs some computation using all the bits of your input value to return some output value with a smaller number of bits.
    2. So the hash function takes a string
      and returns an integer between 0 and
      N .
    3. Now you can look up a pointer to a function in a table of size N.
    4. Each entry in the table will be a linked list (or some other searchable data structure) because of the chance of collision, that is two strings that map to the same hash value.

    E.g.

    lets say hash(char*) returns a value between 0 and 3.
    hash("val1") returns 2
    hash("val2") returns 0
    hash("val3") also returns 0
    hash("val4") returns 1
    

    Now your hash table looks something like:

    table[0] ("val2",foo2) ("val3", foo3)
    table[1] ("val4",foo4)
    table[2] ("val1",foo1)
    table[3] <empty>
    

    I hope you can see how the cost of doing matching using a hash table is bound by the time it takes to calculate the hash function and the small time it takes to search the entry in the hash table. If the hash table is large enough most hash table entries would have very few items.

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

Sidebar

Related Questions

What kind of trick can I use to speed up and improve an image
I've hacked together some php code that can probably be improved in terms of
I need to read some large files (from 50k to 100k lines), structured in
I have an idea of how I can improve the performance with dynamic code
How can I improve this username/password checking? [AcceptVerbs(HttpVerbs.Post)] public ActionResult Login(FormCollection collection) { var
How can I improve this code? What has made this long winded is the
I wonder if anyone can help improve my understanding of JOINs in SQL. [If
I was wondering what settings an application can control to improve battery life in
If I use String.intern() to improve performance as I can use == to compare
I have read that you can do it, but would this really improve performance

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.