Getting rid of for loops?
-
- Posts: 30
- Joined: Thu Dec 29, 2016 10:13 pm
Getting rid of for loops?
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?
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?
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.
-
- Posts: 190
- Joined: Sun Jul 14, 2013 10:00 am
- Real Name: H.G. Muller
Re: Getting rid of for loops?
Indeed. This technique, called 'loop unrolling', is usually done automatically during compilation, when you specify a high-enough optimization level (like -O2 or -O3).
-
- Posts: 30
- Joined: Thu Dec 29, 2016 10:13 pm
Re: Getting rid of for loops?
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.
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.
-
- Posts: 30
- Joined: Thu Dec 29, 2016 10:13 pm
Re: Getting rid of for loops?
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? ![Crying or Very Sad :cry:](./images/smilies/icon_cry.gif)
![Crying or Very Sad :cry:](./images/smilies/icon_cry.gif)
Re: Getting rid of for loops?
Just curious what the speed difference is for you with MSVC 2015 between Debug and Release (with optimization) ?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?
-
- Posts: 30
- Joined: Thu Dec 29, 2016 10:13 pm
Re: Getting rid of for loops?
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.
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?
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.
And, then compiler unrolled Release v manually unrolled Release for same Perft depths.
-
- Posts: 30
- Joined: Thu Dec 29, 2016 10:13 pm
Re: Getting rid of for loops?
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.
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?
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).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?
"Talk is cheap. Show me the code." -- Linus Torvalds.