This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/TMS320F28379D: calltime overhead using getters and setters in classes

Part Number: TMS320F28379D

Tool/software: TI C/C++ Compiler

On the TMS320F28379D,

what is the runtime difference between direct public var acess
foo = myClass.var;

and using getter-methods by making var private

foo = myClass.get_var();

  • I just tried it myself

    class Private_Var {
    public:
    Private_Var(){var = 10;}
    int get_var(){return var;}
    private:
    int var;
    };

    class Public_Var {
    public:
    Public_Var(){var = 10;}
    int var;
    };

    void test_speed(){
    Public_Var public_var;
    Private_Var private_var;
    int dummy = 0;

    for(long i = 0; i < 1000000; i++){
    dummy = public_var.var; //takes 16000009 cycles
    }

    dummy = 0;

    for(long i = 0; i < 1000000; i++){
    dummy = private_var.get_var(); //takes 34000027 cycles
    }

    dummy = 0;
    }

    The get-Method-loop needs about 18000000 cycles more.
    Calculating at 1000000 calls -> one call takes 18 cycles more.

    At 200MHz Clk the get-Method takes 90ns longer .
  • If you build with the compiler option --opt_level=2 or higher, this ...

    Marcel Kummer said:
    foo = myClass.get_var();

    ... is often inlined, and all the overhead is avoided.

    Thanks and regards,

    -George

  • Is there any difference if you declare get-Method as inline?

  • A function defined within a class definition is implicitly inline, according to the C++ standard.

    The compiler will decide to inline functions depending on their perceived benefit, and may not inline those that have been declared "inline." However, getters and setters like these are usually simple enough that it's actually cheaper to have them inlined than not to, especially at --opt_level=2 and higher but also as low as --opt_level=0.
  • At my previous post I compiled at opt-lvl 00

    Now I tried the same with opt-lvl 02.

    With 1.000.000 iterations diong
    var = XXX;
    the needed cycles (including the for loop cycles) are:

    XXX                                       cycles

    local_private.get_var():    12.000.000
    global_privat.get_var():    12.000.000
    local_public.var:                12.000.000
    global_public.var:             12.000.000
    local_variable:                   12.000.000
    global_variable:                 15.000.000  <- no idea why
    local_struct.var:                  12.000.000
    global_struct.var:               12.000.000

  • Marcel Kummer said:
    global_variable:                 15.000.000  <- no idea why

    Accessing a global variable on a C28x device requires the overhead of loading the upper bits of the address in the data page (DP) register.

    Thanks and regards,

    -George