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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:22:13+00:00 2026-05-12T15:22:13+00:00

I have this sort of C function — that is being called a zillion

  • 0

I have this sort of C function — that is being called a zillion times:

void foo ()
{
    if (/*condition*/)
    {

    }
    else if(/*another_condition*/)
    {

    }
    else if (/*another_condition_2*/)
    {

    } 
          /*And so on, I have 4 of them, but we can generalize it*/
    else
    {

    }
 }

I have a good test-case that calls this function, causing certain if-branches to be called more than the others.

My goal is to figure the best way to arrange the if statements to minimize the branching.

The only way I can think of is to do write to a file for every if condition branched to, thereby creating a histogram. This seems to be a tedious way. Is there a better way, better tools?

I am building it on AS3 Linux, using gcc 3.4; using oprofile (opcontrol) for profiling.

  • 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-12T15:22:13+00:00Added an answer on May 12, 2026 at 3:22 pm

    It’s not portable, but many versions of GCC support a function called __builtin_expect() that can be used to tell the compiler what we expect a value to be:

    if(__builtin_expect(condition, 0)) {
      // We expect condition to be false (0), so we're less likely to get here
    } else {
      // We expect to get here more often, so GCC produces better code
    }
    

    The Linux kernel uses these as macros to make them more intuitive, cleaner, and more portable (i.e. redefine the macros on non-GCC systems):

    #ifdef __GNUC__
    #  define likely(x)   __builtin_expect((x), 1)
    #  define unlikely(x) __builtin_expect((x), 0)
    #else
    #  define likely(x)   (x)
    #  define unlikely(x) (x)
    #endif
    

    With this, we can rewrite the above:

    if(unlikely(condition)) {
      // we're less likely to get here
    } else {
      // we expect to get here more often
    }
    

    Of course, this is probably unnecessary unless you’re aiming for raw speed and/or you’ve profiled and found that this is a problem.

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

Sidebar

Related Questions

I have a section of makefile that has this sort of structure: bob: ifdef
I have YAML data that looks sort of like this, but ~150k of it:
There have been a couple of questions that sort of dealt with this but
function safe(){ if($(this).is(':checked')){ $(select[name='sort']).attr(disabled, disabled); $(input[name='group']).attr(disabled, disabled) } else { $(select[name='sort']).attr(disabled, false); $(input[name='group']).attr(disabled, false)
I have this function to sort a JavaScript array of objects based on a
I have this function in vb.net that I converted from C# for a project
I have this big data-entry sort of page, a table kind of layout using
This is sort of SQL newbie question, I think, but here goes. I have
I have this code in jQuery, that I want to reimplement with the prototype
I have this line in a javascript block in a page: res = foo('<%=

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.