#pragma execution_frequency

Description

The #pragma execution_frequency directive lets you mark program source code that you expect will be either very frequently or very infrequently executed.

Syntax

Read syntax diagramSkip visual syntax diagram>>-#--pragma--execution_frequency--(--+-very_low--+--)---------><
                                      '-very_high-'

Notes

Use this pragma to mark program source code that you expect will be executed very frequently or very infrequently. The pragma must be placed within block scope, and acts on the closest point of branching.

The pragma is used as a hint to the optimizer. If optimization is not selected, this pragma has no effect.

Examples

  1. This pragma is used in an if statement block to mark code that is executed infrequently.
    int *array = (int *) malloc(10000);
    
    if (array == NULL) {
        /* Block A */
        #pragma execution_frequency(very_low)
        error();
    }
    The code block "Block B" would be marked as infrequently executed and "Block C" is likely to be chosen during branching.
    if (Foo > 0) {
        #pragma execution_frequency(very_low)
        /* Block B */
        doSomething();
    } else {
        /* Block C */
        doAnotherThing();
    }
  2. This pragma is used in a switch statement block to mark code that is executed frequently.
    while (counter > 0) {
        #pragma execution_frequency(very_high)    	
        doSomething();
    }	/* This loop is very likely to be executed.    */
    
    switch (a) {
        case 1:
            doOneThing();
            break;
        case 2:
            #pragma execution_frequency(very_high)	
            doTwoThings();	
            break;
        default:
            doNothing();
    }    /* The second case is frequently chosen.   */
    
  3. This pragma cannot be used at file scope. It can be placed anywhere within a block scope and it affects the closest branching.
    int a;
    #pragma execution_frequency(very_low)
    int b;
    
    int foo(boolean boo) {
        #pragma execution_frequency(very_low)
        char c;
    
        if (boo) {
            /* Block A */
            doSomething();
            {
                /* Block C */
                doSomethingAgain();
                #pragma execution_frequency(very_low)
                doAnotherThing();
            }
        } else {
            /* Block B */
            doNothing();
        }
    
        return 0;
    }
    
    #pragma execution_frequency(very_low)
    
    The first and fourth pragmas are invalid, while the second and third are valid. However, only the third pragma has effect, and it affects whether program execution branches to Block A or Block B during the decision of "if (boo)". The second pragma is ignored by the compiler.