473,327 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Lexical variables - speed penalty?


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/
Nov 14 '05 #1
3 1389
In article <bj*************@termo1.fi.uib.no>,
Joakim Hove <ho**@ift.uib.no> 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
Nov 14 '05 #2

"Joakim Hove" <ho**@ift.uib.no> 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.
Nov 14 '05 #3
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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matt Knepley | last post by:
I must be misunderstanding how Python 2.3 handles lexical scoping. Here is a sample piece of code: def run(): a = 1 def run2(b): print a run2(2) print a run()
34
by: Jacek Generowicz | last post by:
I have a program in which I make very good use of a memoizer: def memoize(callable): cache = {} def proxy(*args): try: return cache except KeyError: return cache.setdefault(args,...
0
by: Collin VanDyck | last post by:
Hello! I have set up a pluggable SAX transformation pipeline which is made up of individual nodes that transform the source XML, and for the most part it works well. Each node in the pipeline...
25
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use...
5
by: JENS CONSER | last post by:
Hello NG, We have a performance problem in using a client server solution based on MS SQL-Server 2000 through a VPN tunnel (via broadband internet connection). The SQL Server is running on a...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
14
by: Khookie | last post by:
Woah... is it just me or do C programmers don't bother talking about how cool C can be (compared to Lisp, Haskell, etc.) - functionally speaking? // Lexical scoping - via nested functions...
3
by: globalrev | last post by:
i cant figure outif python has lexical or general scope. it seems functions have lexical scope but with some restrictions and some non-function scopes are dynamic?
6
by: Patrick Sullivan | last post by:
Hello. I will be using some large data sets ("points" from 2 to 12 variables) and would like to use one class for each point rather than a list or dictionary. I imagine this is terribly...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.