Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: question about local and global variables

Author: Dieter Buerssner

Date: 09:25:44 09/02/01

Go up one level in this thread


I think, one shouldn't use global variable, when not really needed.

Usually local variables are faster, because they can be stored in registers.
The optimize can often not store global variable in registers on the PC
architecture. Also, it often cannot store them in registers, because it cannot
(easily) prove, that the values are unchanged. Assume

  for (i=0; i<N; i++)
    func();

With i global, the compiler cannot know, if func() actually changes i. With a
local i, the compiler know, that i will not be changed by func(), and it can
store it in a register, that is not changed by a function call.

On the PC-architecture there are only few free registers. So, for any slightly
complicated function, the optimizer has to do some guesswork, which variable are
best kept in registers. Often it will do this of course not in an optimal way.
In this case, using some global variable, may help the compiler to do some
better guessing. But a small modification to the code may yield again in
different results.

Another point is, where the data is actually stored. Adding some global data,
may yield in better or worse cache efficiency. Again, a slight modification may
make things totally different again.

Regards,
Dieter




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.