Page 1 of 2
Getting rid of for loops?
Posted: Tue Feb 07, 2017 4:32 pm
by notachessplayer
I was wondering if it is common in chess programming, in order to make the program faster, to replace "for" loops with plain code when it is possible?
For example, instead of looping through 8 directions (indexed to a direction array) for my queen, I type the same code 8 times with the 8 different directions.
Just by doing so for all pieces, I went from ~1.47s to ~1.33s for a perft 6.
Since I chose to give my pieces a dead flag, I never remove them from my pieces list, so I can do the same with my pieces by typing the same code 16 times.
The code is now obviously disgusting, but the results make me consider keeping it that way. I had never realized how slow looping actually is.
Thoughts?
Re: Getting rid of for loops?
Posted: Tue Feb 07, 2017 5:48 pm
by BrianR
I suggest using a compiler with optimization capabilities. All of them are very very good these days. It is better to focus on what your code does than to worry about how to do it. The compilers will automatically optimize it. If you try to do so yourself, the likelihood of introducing a bug will skyrocket. Perft will end up consuming only about 10% of your engine's thinking time (certainly less than 20%), so even if you were able to optimize it theoretically to close to zero, the overall time savings is almost nil relative to evaluation and searching.
Re: Getting rid of for loops?
Posted: Tue Feb 07, 2017 7:53 pm
by H.G.Muller
Indeed. This technique, called 'loop unrolling', is usually done automatically during compilation, when you specify a high-enough optimization level (like -O2 or -O3).
Re: Getting rid of for loops?
Posted: Wed Feb 08, 2017 12:44 am
by notachessplayer
As ashamed as I am to admit it, I had never thought about a compiler being an important part of a program's capabilities.
I'm currently developing/compiling with VS community 2015, on windows 7.
Mind sharing "the" best compiler that I could use, when it comes to execution speed? I would gladly go back to my slow yet elegant "for" loops if a compiler allows me to do so.
Re: Getting rid of for loops?
Posted: Wed Feb 08, 2017 11:48 pm
by notachessplayer
I tried the GCC compiler, with the -O3 option flag, but the program is slower than the one compiled in VS. If VS doesn't unroll my loops, and g++ is slower, then what do?
Re: Getting rid of for loops?
Posted: Sun Feb 12, 2017 3:19 pm
by BrianR
notachessplayer wrote:I tried the GCC compiler, with the -O3 option flag, but the program is slower than the one compiled in VS. If VS doesn't unroll my loops, and g++ is slower, then what do?
Just curious what the speed difference is for you with MSVC 2015 between Debug and Release (with optimization) ?
Re: Getting rid of for loops?
Posted: Sun Feb 12, 2017 4:15 pm
by notachessplayer
The difference is huge.
A perft 5 is 1,3s with debug and a perft 6 is 1,05s with release.
I think it has to do with the fact that I manually unrolled my loops? Just a guess.
Btw, I have read about the intel c++ compiler. It seems to have a good reputation and they claim to be a better option than the VS compiler.
Re: Getting rid of for loops?
Posted: Sun Feb 12, 2017 5:04 pm
by BrianR
Sorry, I meant letting the compiler unroll the loops Debug v Release.
And, then compiler unrolled Release v manually unrolled Release for same Perft depths.
Re: Getting rid of for loops?
Posted: Tue Feb 14, 2017 2:44 am
by notachessplayer
I looked into it but couldn't find where to ask the compiler to unroll loops.
Btw, I didn't work with git on this project, and deleted obsolete versions as I coded so I don't have my code with loops anymore.
Re: Getting rid of for loops?
Posted: Sat Feb 18, 2017 7:42 am
by lucasart
notachessplayer wrote:I tried the GCC compiler, with the -O3 option flag, but the program is slower than the one compiled in VS. If VS doesn't unroll my loops, and g++ is slower, then what do?
I really doubt that gcc is slower than the microsoft compiler. Perhaps you forgot to use -DNDEBUG with gcc (or equivalently, #define NDEBUG), resulting in gcc doing a debug compile. Also, you need to use -flto (compile and link flag).