Getting rid of for loops?

Code, algorithms, languages, construction...
notachessplayer
Posts: 30
Joined: Thu Dec 29, 2016 10:13 pm

Getting rid of for loops?

Post by notachessplayer » Tue Feb 07, 2017 4:32 pm

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?

BrianR
Posts: 17
Joined: Thu Jun 10, 2010 4:48 am

Re: Getting rid of for loops?

Post by BrianR » Tue Feb 07, 2017 5:48 pm

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.

H.G.Muller
Posts: 190
Joined: Sun Jul 14, 2013 10:00 am
Real Name: H.G. Muller

Re: Getting rid of for loops?

Post by H.G.Muller » Tue Feb 07, 2017 7:53 pm

Indeed. This technique, called 'loop unrolling', is usually done automatically during compilation, when you specify a high-enough optimization level (like -O2 or -O3).

notachessplayer
Posts: 30
Joined: Thu Dec 29, 2016 10:13 pm

Re: Getting rid of for loops?

Post by notachessplayer » Wed Feb 08, 2017 12:44 am

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.

notachessplayer
Posts: 30
Joined: Thu Dec 29, 2016 10:13 pm

Re: Getting rid of for loops?

Post by notachessplayer » Wed Feb 08, 2017 11:48 pm

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? :cry:

BrianR
Posts: 17
Joined: Thu Jun 10, 2010 4:48 am

Re: Getting rid of for loops?

Post by BrianR » Sun Feb 12, 2017 3:19 pm

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? :cry:
Just curious what the speed difference is for you with MSVC 2015 between Debug and Release (with optimization) ?

notachessplayer
Posts: 30
Joined: Thu Dec 29, 2016 10:13 pm

Re: Getting rid of for loops?

Post by notachessplayer » Sun Feb 12, 2017 4:15 pm

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.

BrianR
Posts: 17
Joined: Thu Jun 10, 2010 4:48 am

Re: Getting rid of for loops?

Post by BrianR » Sun Feb 12, 2017 5:04 pm

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.

notachessplayer
Posts: 30
Joined: Thu Dec 29, 2016 10:13 pm

Re: Getting rid of for loops?

Post by notachessplayer » Tue Feb 14, 2017 2:44 am

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.

lucasart
Posts: 201
Joined: Mon Dec 17, 2012 1:09 pm
Contact:

Re: Getting rid of for loops?

Post by lucasart » Sat Feb 18, 2017 7:42 am

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? :cry:
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).
"Talk is cheap. Show me the code." -- Linus Torvalds.

Post Reply