Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: question about local and global variables

Author: Rafael Andrist

Date: 08:47:55 09/02/01

Go up one level in this thread


On September 02, 2001 at 07:29:12, Uri Blass wrote:

>My question is when it is a good idea to define global variables
>instead of local variables
>
>There are some variables in my move generator that originally
>were defined only as local variable in some procedures
>(I do not need their value when I am not in the  procedures)
>
>I found a trick to make my program slightly faster by defining them
>also as global variable and not using them as local variables in part
>of the cases.
>My question is if there is a rule to know when to delete them
>as local variables
>I found that in part of my procedures using them as global
>varaibles make my program sligtly faster.
>when in another part of the procedures
>it makes my program slightly slower.
>
>Is there a way to know when it is faster
>without testing for every variable?
>
>I use visual studio 6 and I also chose
>optimize for speed in my project.
>
>Uri

This happens also to me. I don't think that there's a general rule, but I can
give you the following 2 explications:

1) a procedure is called very often. Using a local variable, you need time to
allocate/deallocate it or the adress calculation of the local variable may need
more time than the one for a global variable.

2) if you want to use the specific global variable, it is not in the cache and
you get a penalty. Between the procedure call the cache is allways filled with
other stuff.

Rafael B. Andrist



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.