Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: memory performance, memory allocation under windows questions

Author: Bo Persson

Date: 14:26:43 12/10/01

Go up one level in this thread


On December 10, 2001 at 06:13:13, Dieter Buerssner wrote:

>On December 09, 2001 at 21:09:17, martin fierz wrote:
>
>>i have written a checkers interface and a checkers engine, and recently i made a
>>change so that the user can set the hashtable size in the interface and it is
>>communicated to the engine.
>>up to now, my code looked like this:
>>
>>// global variables
>>struct hashentry hashtable[BIGNUMBER];
>>
>>now it looks like this:
>>
>>// global variables
>>struct hashentry *hashtable;
>>
>>// initialization
>>hashtable = malloc(BIGNUMBER*sizeof(struct hashentry));
>>
>>later in the code i access the hashtable just as before with hashtable[i].
>>i would have expected these two approaches to be equivalent, but the new one
>>drops a few % of speed overall.
>
>While the C code accessing the table looks the same, the machine code will be
>different. The pointer approach needs one indirection more (the pointer must be
>loaded into a register, while adressing. The array will be accessed without this
>indirection. It has a constant adress in memory, that is known at link time).
>
>But still, your few percents look a bit much. Because the adressing should take
>much less time, than the actual memory access.
>
>I have seen such figures too, and wondered. In my case, interestingly some
>malloced memory seemed faster.

Yes, I have seen that too. Sometimes the optimizer will keep the pointer in a
register thru the entire function, and save space and time by using smaller
instructions (without the adress).

mov ax,[cx + bx]

is smaller/faster than

mov ax,array[bx]

especially if it cane be used several times.

>Regards,
>Dieter


Bo Persson
bop2@telia.com



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.