Page 1 of 1

Slow code only

Posted: Thu Jan 24, 2019 4:44 pm
by Henk
This code works for iterating over squares in C#. Just use foreach.

Code: Select all

            fieldIterator.Reset(pos.Board.Pawns);
            foreach(var field in fieldIterator)
            {  
            }     


    public class FieldIterator : IEnumerator<IField>
    {
        ulong iniBits;
        UInt64 bits;
        ulong bit;
        IBoardSquares board;

        public IField Current
        {
            get
            {
                return board[bit];

            }
        }

        object IEnumerator.Current
        {
            get
            {
                return bit;
            }
        }

       

 
        BitBoardIndex bbIndex = BitBoardIndex.Instance;

        public FieldIterator( IBoardSquares board)
        {
            this.board = board;
        }


        public FieldIterator(UInt64 target, IBoardSquares board)
        {
            iniBits = target;
            bits = iniBits;
           
            this.board = board;
        }

        public void Reset(UInt64 target)
        {
            iniBits = target;
            bits = iniBits;
        }

     

        public void Dispose()
        {
        }

        public bool MoveNext()
        {
            bit = bits & (~bits + 1);
            bits &= bits - 1;
            return bit != 0;
        }

        public void Reset()
        {
            bits = iniBits;
        }

        public FieldIterator GetEnumerator()
        {
            return this;
        }       
    }
    

Re: Slow code only

Posted: Thu Jan 24, 2019 5:29 pm
by Henk
Don't know yet if you can make it slower using lambda expressions.

Re: Slow code only

Posted: Fri Jan 25, 2019 4:46 pm
by Henk
Easy to make it even more slow. You have to collect the squares into a collection before you can apply lambda expressions.


For instance:

Code: Select all

           var target = pos.Board.GetBits(Kind.Rook_Kind, colorSign);          
            fieldIter.Reset(target);
            var rookFieldList = new List<IField>(fieldIter.Fields());
            if (testCastledShort)
            {
                return !rookFieldList.Exists(rookField => rookField.ColNr > kingColNr);
            }
            
class FieldIterator
{        
        ..  
        public IEnumerable<IField> Fields()
        {
            foreach (var field in this)
            {
                yield return field;
            }
        }
}

  

Re: Slow code only

Posted: Sat Jan 26, 2019 10:37 am
by Henk
I think this is another less is slow improvement. Idea is to use LINQ.

Code: Select all

            fieldIter.Reset(pos.Board.GetBits(Rook_Kind, colorSign));                   
            if (testCastledShort)
            {
              	return !(from u in fieldIter.Fields() let v = kingColNr where u.ColNr > v select u.ColNr).Any();
            }
 

Re: Slow code only

Posted: Wed Feb 06, 2019 12:51 pm
by Henk
LINQ. Slow but compact. Computing space here:

Code: Select all

    double result = (from field in fieldIterator.Fields()
                            let occupier = field.Occupier
                            let space = field.Space(occupier.Colour)
                            select occupier.ColorSign2 * space).Sum();
 

Re: Slow code only

Posted: Fri Feb 15, 2019 2:21 pm
by Henk
KiwiPete(5) takes one minute on my computer. Don't know how fast or slow it had been in the past.

[d] r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1

Re: Slow code only

Posted: Fri Feb 15, 2019 2:25 pm
by xr_a_y

Code: Select all

# +-+-+-+-+-+-+-+-+
# |r| | | |k| | |r|
# +-+-+-+-+-+-+-+-+
# |p| |p|p|q|p|b| |
# +-+-+-+-+-+-+-+-+
# |b|n| | |p|n|p| |
# +-+-+-+-+-+-+-+-+
# | | | |P|N| | | |
# +-+-+-+-+-+-+-+-+
# | |p| | |P| | | |
# +-+-+-+-+-+-+-+-+
# | | |N| | |Q| |p|
# +-+-+-+-+-+-+-+-+
# |P|P|P|B|B|P|P|P|
# +-+-+-+-+-+-+-+-+
# |R| | | |K| | |R|
# +-+-+-+-+-+-+-+-+
# wk e1
# bk e8
# Turn white
# Phase 1
# Static score 84
# Hash 17569928883477024425
# FEN r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1
# Info  2019-02-15 14:24:36-678: pseudoNodes   195934515
# Info  2019-02-15 14:24:36-678: validNodes    193690690
# Info  2019-02-15 14:24:36-678: Starting worker 0
# Info  2019-02-15 14:24:36-678: Waiting for workers to join...
# Info  2019-02-15 14:24:36-678: ... ok threadPool deleted
 
real    0m14,421s
user    0m14,172s
sys     0m0,240s