Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: question about local and global variables

Author: Robert Hyatt

Date: 06:44:53 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 a few issues:

1.  If you want a variable to maintain its "state" across procedure calls,
then it should be either global, or static in that procedure.  There is no
real difference between the two.

2.  If you want a variable to be visible in multiple procedures, then you
can either make them global for the entire program, or just global to a
specific set of procedures, whichever you prefer.

3.  If you have plans for a parallel search, and you modify global variables
as part of your tree search, this won't work in a parallel search, because
the multiple threads will share those global variables making them useless
if they are being modified, unless you start locking/unlocking them.

There is no performance issue at all in the use of global and local
variables, if they are used in a single procedure.  Otherwise you need some
way to let multiple procedures see a local variable, which generally means
a formal parameter in a procedure call, which can hurt performance dur to the
lack of registers on the X86 processors.



>
>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 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.