Lexical variables - speed penalty?

Discussion in 'C Programming' started by Joakim Hove, Oct 7, 2004.

  1. Joakim Hove

    Joakim Hove Guest

    Hello,

    i have a quite large and unwieldy function with *many* local
    variables. The various variables are typically only used in small
    parts of the function, and I would like to define them locally:


    double function () {

    double gvar1,gvar2;

    {
    double var1,var2;

    /* Code doing something with var1 and var2 */

    }

    /* Code accessing the function-global variables gvar1 and gvar2 */

    {
    double var3,var4;

    /* Code working on var3 and var4 */

    }

    }


    The code path is deterministic, and all blocks will be executed. Now,
    my question is wether there is a hit in execution speed by doing it
    like this?


    Regards

    Joakim

    --
    Joakim Hove
    hove AT ift uib no
    +47 (55 5)8 27 90
    http://www.ift.uib.no/~hove/
    Joakim Hove, Oct 7, 2004
    #1
    1. Advertising

  2. In article <>,
    Joakim Hove <> wrote:

    >The code path is deterministic, and all blocks will be executed. Now,
    >my question is wether there is a hit in execution speed by doing it
    >like this?


    No, a good compiler will re-use the same registers for variables with
    different scopes (assuming it has enough registers anyway). There's
    no reason for it to produce code any different from what you'd get if
    you re-used variables yourself, and in some (rare) cases it may even
    produce better code because it can be sure about when the values are
    no longer required.

    -- Richard
    Richard Tobin, Oct 7, 2004
    #2
    1. Advertising

  3. Joakim Hove

    Malcolm Guest

    "Joakim Hove" <> wrote
    >
    > i have a quite large and unwieldy function with *many* local
    > variables. The various variables are typically only used in small
    > parts of the function, and I would like to define them locally:
    >

    Probably there won't be any difference in execution speed. This is because
    compilers typically set up the stack frame at the beginning of each
    function, not for each block. So your variables local to the block are just
    expanded to function variables.
    There is naturally no guarantee.
    Malcolm, Oct 7, 2004
    #3
  4. Joakim Hove

    CBFalconer Guest

    Joakim Hove wrote:
    >
    > i have a quite large and unwieldy function with *many* local
    > variables. The various variables are typically only used in small
    > parts of the function, and I would like to define them locally:


    You should break it up into multiple functions. It will read much
    better, and can even generate the same code if you use the inline
    directive (C99 and gcc).

    Doing that can also handle the local initializations, because
    function parameters are just initialized local variables. So many
    of the variables should disappear. The paramatization and breakup
    may well identify common code subsets.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Oct 7, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Bär
    Replies:
    2
    Views:
    418
    Yan-Hong Huang[MSFT]
    Jul 18, 2003
  2. Michael Andersson

    Exceptions performance penalty

    Michael Andersson, Sep 2, 2003, in forum: C++
    Replies:
    7
    Views:
    549
    Oliver S.
    Sep 3, 2003
  3. Yuri Victorovich

    Performance penalty for encapsulations ??

    Yuri Victorovich, Sep 6, 2003, in forum: C++
    Replies:
    1
    Views:
    336
    Kevin Goodsell
    Sep 6, 2003
  4. A. Saksena
    Replies:
    9
    Views:
    429
    Peter Koch Larsen
    Sep 14, 2004
  5. alex goldman
    Replies:
    8
    Views:
    432
    alex goldman
    May 31, 2005
Loading...

Share This Page