krunch wrote: ↑Tue Jun 07, 2022 12:23 am
Update: Thanks to krunch we now have faster C# testing code.
I quite like the idea to use a class as a template and then switching on it. All ideas I had involved Activator.CreateInstance<T> and then you get a class instance - calling a member on that instance involves a virtual call (which is much slower than calling static members).
I also added Unrolled loops and even faster - direct pointer access:
https://github.com/Gigantua/Chess_Moveg ... 20edaff93b
Oh and the Func jumptable in C#: (array of 64x functions)
Code: Select all
public static Func<ulong, ulong>[] GetSliderHCond = new Func<ulong, ulong>[]
{
(occupy) => {
ulong result = 2ul;
if ((occupy & 2ul) == 0) result |= 4ul;
if ((occupy & 6ul) == 0) result |= 8ul;
if ((occupy & 14ul) == 0) result |= 16ul;
if ((occupy & 30ul) == 0) result |= 32ul;
if ((occupy & 62ul) == 0) result |= 64ul;
if ((occupy & 126ul) == 0) result |= 128ul;
return result;
},
....
};
is not faster than a switch (on my laptop)
Code: Select all
Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz
CSharp Optimized Test Loop
*Switch 15,21
*Switch_Jumptable 14,21
*ObstructionDiff 135,20
*Leorik 133,00
*HyperbolaQsc 167,80
*FancyMagic 219,29
*FancyMagicUnsafe 320,36
*Pext 326,31
*PextUnsafe 413,54
For a chess engine in C# I would have the feeling of being hold back - when normal array accessors make any program much slower comapred to pointer array access.
At least native (unsafe) C# can currently be 80% as fast as C++ for these kinds of algorithms which is still very good.