Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: a question about the advantage of short code for tasks that are done

Author: José Carlos

Date: 10:40:39 04/11/03

Go up one level in this thread


On April 11, 2003 at 11:59:42, Ulrich Tuerke wrote:

>On April 11, 2003 at 11:09:47, Uri Blass wrote:
>
>>only one time or not done often.
>>
>>I have task that I do only one time and I may do my code shorter
>>by adding functions.
>>
>>Note that they are a small minority of the code so
>>I do not expect a big reduction.
>>
>>Here are some examples:
>>
>>1)In the code that build the array of knightmoves I have
>>8 similiar if.
>>
>>if (fil>=2&&rank>=1)
>>		{
>>			knightmove[sq][i]=index(fil-2,rank-1);
>>			direction[sq][knightmove[sq][i]]=dknight;
>>			i++;
>>		}
>>
>>Is there an advantage in changing it to something like
>>
>>if (fil>=2&&rank>=1)
>>{
>>buildknightarrays(sq,i,fil-2,rank-1);
>>i++;
>>}
>>
>>or maybe it is even better to give buildknightarrays
>>a pointer to i to save the i++ that I do 8 times.
>>
>>
>>2)In my code for setup position I have the following code
>>
>>	case 'n':
>>				info[square]+=KNIGHT;
>>				valuepieces+=3;
>>				if (color(square)==LIGHT)
>>					valpieces[0]+=3;
>>				else
>>					valpieces[1]+=3;
>>				break;
>>
>>I can replace 3 by piece_value[KNIGHT]
>>replace 0 by LIGHT and replace 1 by DARK
>>
>>I can also have a function
>>putpiece(int square,int piece)
>>{
>>info[square]+=piece;
>>valuepieces+=valpieces[piece];
>>if (color(square)=LIGHT)
>>  valpieces[LIGHT]+=valpieces[piece];
>>else
>> valpieces[DARK]+=valpieces[piece];
>>}
>>
>>Questions:
>>
>>
>>1)Is there an advantage for doing these things except doing the code more easy
>>to read?
>
>I don't think so. It's solely for easy readability, and perhaps also to ease
>maintenance.
>A function call even generates a tiny runtime overhead. However, some compilers
>offer to define "inline" functions; in this case there is no runtime penalty.

  That's correct of course. Only exception might be if the code block is big and
it is used in a lot of places. Inlining it might actually hurt performance
because of cache faults.

  José C.


>>2)Is it better to add a special file to the project of things that I do
>>not do often(like initializing arrays every time that I setup a new FEN)?
>
>I think that it may make sense to collect the performance-critical functions in
>modules. It could be easier to load the performance critical code into the
>cache. However I think that this may depend a lot on your build environment.
>
>Uli
>
>>
>>I remember a claim of christhope that he tried to limit the
>>things that the program does often to 64k or 128k(I am not sure about the
>>number) but I have no idea how much memory movei use for the main things that
>>it does.
>>
>>The main things that it does include the move generator
>>and the search but both things are done in different files in the 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.