Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Question about static vs global variables

Author: Gareth McCaughan

Date: 04:12:21 10/28/01

Go up one level in this thread


Matthias Gemuh wrote:

>On October 27, 2001 at 06:58:49, Gareth McCaughan wrote:
>
>>Frank Phillips wrote:
>>
>>> Are there any issues associated with time?  Presumably at runtime
>>> storage has to be created for local variables in a recursive function,
>>> such as ABSearch(), and then destroyed; whereas it has to be created
>>> only at initialisation for globals.
>>
>>No, it's basically free. A recursive function call
>>(actually, almost any function call) will involve
>>making some space on the stack to store things; local
>>variables just get some space there. The only "creation"
>>needed is adding a slightly larger number to the stack
>>pointer. <pedant>Which, I suppose, may mean a very
>>slightly larger number of page faults and slightly
>>worse cache performance, but so will the extra space
>>needed by globals *all* the time.</pedant>
>
>
> This logic doesn't look sound to me. Every recursion has to allocate
> (and later free) space for local (non-static) variables. Each recursion
> DOES NOT reallocate memory for global variables, so there should be
> a perfomance issue.

The "allocation" doesn't cost anything (in time; it does of course
cost some space). The function call needs some space in the stack
*anyway*, and introducing new local variables just changes the amount
that gets added to the stack pointer.

If your function is *very* simple -- so simple that without
the local variables it would only be using onr or two registers --
then using local variables does require some extra stack activity.
But if you're spending enough time calling such simple functions
that it matters how many cycles they take, you should be inlining
them and then this won't be an issue at all.

--
g



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.