Stockfish compiled by Visual Studio is much slower than by Cygwin?

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
phhnguyen
Posts: 1541
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Stockfish compiled by Visual Studio is much slower than by Cygwin?

Post by phhnguyen »

I have tried to compile Stockfish by both Cygwin64 and Visual Studio community 2017 (ver 15.5.5).

For Cygwin64 I compiled with command line: make build ARCH=x86-64. It compiled without any problem and Stockfish bench gives me about 1.8 MNPS.

For VS, I created a new project, add all source files, then open Property dialog, select Release / x64 platform and add -DNDEBUG to "Additional Options" (sometimes I have tried with -DUSE_POPCNT and /O3). The VS can compile without any problem. However Stockfish bench says about 800 KNPS only, just 44% of Cygwin64's one.

That surprised me since I thought VS usually is better than gcc/g++ to create faster executive files for Windows.

My computer is Intel Core i7, Win10 pro 64 bit.

Did I miss something? Anyway to improve Stockfish speed with VS? Thanks
https://banksiagui.com
The most features chess GUI, based on opensource Banksia - the chess tournament manager
MOBMAT
Posts: 414
Joined: Sat Feb 04, 2017 11:57 pm
Location: USA

Re: Stockfish compiled by Visual Studio is much slower than by Cygwin?

Post by MOBMAT »

I only use VS, so I have no reference point.

For compiling for release (no debug), try a few of these settings. I compile out of the IDE, not the command line.

Right click on your project in the Solution Explorer and select Properties (at the bottom of the list, usually).
In the Property Pages, select C/C++ (i'm assuming you coding in C), and click on Optimization.

on the right side, set the following:

Optimization (/O2) favor speed
Inline Function Expansion (Default, though I might try /Ob2, now that I see it)
Favor Size or Speed - Favor fast code (/Ot)
Whole Program Optimization - Yes (/GL)

EDIT: Oh, if you have/need Intrinsics, set that value to Yes (/Oi)

Leave everything default.

See if that helps.
i7-6700K @ 4.00Ghz 32Gb, Win 10 Home, EGTBs on PCI SSD
Benchmark: Stockfish15.1 NNUE x64 bmi2 (nps): 1277K
User avatar
phhnguyen
Posts: 1541
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Re: Stockfish compiled by Visual Studio is much slower than by Cygwin?

Post by phhnguyen »

Thanks for the suggestions.

I have tried. Actually the default optimisation is set already to O2 (maximize speed). Thus I have changed others as your suggestion.

Look a bit better (the speed is about 900 KPS) but still half of Cygwin (over 1.8MPS).


In the Properties Dialog, All Options as bellow:

Code: Select all

/permissive- /GS /GL /W3 /Gy /Zc:wchar_t /Zi /Gm- /O2 /Ob2 /sdl /Fd"x64\Release\vc141.pdb" /Zc:inline /fp:precise /D "_MBCS" /errorReport:prompt /WX- /Zc:forScope /Gd /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Ot /Fp"x64\Release\sf.pch" /diagnostics:classic 
Additional Options:

Code: Select all

-DNDEBUG
https://banksiagui.com
The most features chess GUI, based on opensource Banksia - the chess tournament manager
jdart
Posts: 4429
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: Stockfish compiled by Visual Studio is much slower than by Cygwin?

Post by jdart »

Flags I use with VC++:

cl /Ox /Gr /Gy /Oi /GS- /GL /DNDEBUG /D_WIN64 /GA /GF /EHsc /D_CONSOLE /D_CRT_SECURE_NO_DEPRECATE /GF /W3 /MT

--Jon
pferd
Posts: 134
Joined: Thu Jul 24, 2014 2:49 pm

Re: Stockfish compiled by Visual Studio is much slower than by Cygwin?

Post by pferd »

I tried using MSVC before. When I type stockfish bench.exe it is very slow. Redirecting most of the output with stockfish bench > nul regains most of speed loss,
Joost Buijs
Posts: 1688
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: Stockfish compiled by Visual Studio is much slower than by Cygwin?

Post by Joost Buijs »

pferd wrote: Wed Jun 27, 2018 3:35 pm I tried using MSVC before. When I type stockfish bench.exe it is very slow. Redirecting most of the output with stockfish bench > nul regains most of speed loss,
I noticed this too, the bench produces a lot of output in a very short time and this consumes a lot of time in the Windows(10?) console. The compiler is not the problem, with the Intel compiler (using the same backend) the results are about the same. Actually a weakness of the Stockfish bench, when you want to test search and evaluation performance the console output should be as minimal as possible.

If you have a processor that supports AVX2, making an AVX2 build with 'arch:AVX2' really helps and turning off the security check with 'GS-' gives another few percent.

These are the flags that I use with MSVC:

Code: Select all

/permissive- /GS- /GL /W3 /Gy /Zc:wchar_t /Zi /Gm- /O2 /sdl /Fd"x64\Release\vc141.pdb" /Zc:inline /fp:precise /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /errorReport:prompt /WX- /Zc:forScope /arch:AVX2 /Gd /Oi /MT /FC /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Ot /Fp"x64\Release\Stockfish9.pch" /diagnostics:classic
User avatar
phhnguyen
Posts: 1541
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Re: Stockfish compiled by Visual Studio is much slower than by Cygwin?

Post by phhnguyen »

pferd wrote: Wed Jun 27, 2018 3:35 pm I tried using MSVC before. When I type stockfish bench.exe it is very slow. Redirecting most of the output with stockfish bench > nul regains most of speed loss,
You are right - I see its speed now is close (a bit slower) to Cygwin's one.

I am wondering:
1) Why Stockfish exe compiled by VS differs from one by Cygwin/g++ when the source code is the same for both?
2) When running Stockfish under a GUI, does it become slower since all its output are redirected to other program but not null?
https://banksiagui.com
The most features chess GUI, based on opensource Banksia - the chess tournament manager
User avatar
phhnguyen
Posts: 1541
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Re: Stockfish compiled by Visual Studio is much slower than by Cygwin?

Post by phhnguyen »

I can answer myself for question 2. I have loaded both exe files (compiled by VS and Cygwin) into Winboard and analyse few positions. Speed reports say they all work well (not any crazy slow).

However I see the speed of VS exe is always slower than Cygwin's one (about 10% - a significant amount), not sure that is caused by un-optimised compilers or VS worse. I am not good at compiler optimisation. If someone could, please check.
https://banksiagui.com
The most features chess GUI, based on opensource Banksia - the chess tournament manager
pferd
Posts: 134
Joined: Thu Jul 24, 2014 2:49 pm

Re: Stockfish compiled by Visual Studio is much slower than by Cygwin?

Post by pferd »

Stockfish has been developed and optimised mainly with gcc so I would expect it to run fastest with that compiler.

I toyed around and my best version was around 7% slower than the one I downloaded from abrok.eu
User avatar
phhnguyen
Posts: 1541
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Re: Stockfish compiled by Visual Studio is much slower than by Cygwin?

Post by phhnguyen »

I have played a bit with Stockfish code, focusing on the way it prints output. I suspected the code sync_cout (defined as std::cout + IO_LOCK) as the cause to slow down Stockfish when it runs alone in the console.

My first attempt was to replace them all by simple code std::cout (without io locks, BTW, I don't think Stockfish needs io lock here since it prints output from the main thread only) - the speed was the same (slow - half speed). However when I replaced them with C function printf (without io locks either), it gained back all losing speed.

I know it is not a big deal since Stockfish can work well with gcc compilers and/or with GUIs. But it is better if its team or someone review those codes.
https://banksiagui.com
The most features chess GUI, based on opensource Banksia - the chess tournament manager