Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Lexical variables - speed penalty?

Reply
Thread Tools

Lexical variables - speed penalty?

 
 
Joakim Hove
Guest
Posts: n/a
 
      10-07-2004

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/
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      10-07-2004
In article <(E-Mail Removed)>,
Joakim Hove <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      10-07-2004

"Joakim Hove" <(E-Mail Removed)> 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.


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-07-2004
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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to dump all (lexical) variables? kj Perl Misc 2 09-29-2009 03:15 PM
Lexical variables in (?{...}) regexp constructs Ala Perl Misc 5 06-16-2009 11:55 PM
Warning about unused lexical variables Peter J. Holzer Perl Misc 5 09-07-2007 02:54 PM
speed speed speed a.metselaar Computer Support 14 12-30-2003 03:34 AM
Style question regarding subroutines and lexical variables Joseph Ellis Perl Misc 6 07-24-2003 11:37 PM



Advertisments