20

Just tests a very simple command like:

while true; do bash -c "echo hello"; done

You will find how much slow the bash in Cygwin is. Does anybody know why?

It's a fresh install of cygwin 1.7 on win7.

thanks to Jared's testing idea, I modified the command to this(adds bash -c):

time for i in {1..10} ; do bash -c "echo Hello" ; done
Hello
...
real 0m7.711s //it's the problem
user 0m0.091s
sys 0m0.476s
6
  • 1
    Maybe the trouble is simply that Windows 7 can't create processes very fast? Commented Jul 25, 2011 at 3:28
  • 8
    Windows can't fork processes very fast. In fact, it doesn't fork at all. Cygwin fudges it, but can't do it anywhere near as efficiently as a real *nix can. Each process is starting from scratch, and has to load and init the cygwin DLL, libc, etc that a fork-and-go system already has loaded.
    – cHao
    Commented Jul 25, 2011 at 4:31
  • But the strange is that during the command creating the 10 processes for about 7.7 seconds, the CPU is empty and you can see that the user & sys time is slightly. It looks like bash is waiting for something, but what does it waiting for?
    – Lcsky
    Commented Jul 25, 2011 at 5:14
  • 1
    I get 0.970s real, 0.090s user, 0.307s sys. (I thought it might be a startup script, but bash -c ... doesn't invoke any startup scripts.) strace -o bash.strace bash -c "echo hello" might tell you something useful. Commented Aug 2, 2011 at 0:42
  • > time ls -la ~/c/cygwin64/bin gave me an unimaginable 3m46.373s!! It was always slow, but recently our IT dept. changed the Windows login domain and usernames, and since then all hell broke lose...
    – ysap
    Commented Sep 9, 2016 at 16:32

11 Answers 11

16

How about excluding Cygwin paths from your antivirus software ?

10
  • Which paths are cygwin paths ? I excluded this one - C:\cygwin64. Btw, for some antiviruses such as norton, you can find the "exceptions" option somewhere and add this folder to the exceptions list.
    – Steam
    Commented Jan 1, 2015 at 0:22
  • Should I also add the folder to which cygwin downloads all its packages ? That is, the one with the weird name like - ftp%3a%2f%2fftp.harvard.univ.edu%2fpub%2fcygwin%2f
    – Steam
    Commented Jan 1, 2015 at 0:29
  • As far as I know, the downloaded packages folder is used only for caching downloaded packages and installatin/reinstallation with Cygwin setup. Those files are not being used when running cygwin. I think that c:\cygwin64 should be sufficient. I'd check Windows system folders for cygwin DLLs too. Not that I'd suggest adding vital Windows folders to AV exceptions :)
    – dotz
    Commented Jan 1, 2015 at 9:59
  • I use Avast antivirus. Cygwin is excruciatingly slow to do any operation. Disabling the Avast shield makes Cygwin very fast. Commented Mar 24, 2016 at 4:09
  • In my case, I had a really poor performing PS1 (custom prompt) variable set. Once I removed it, everything was really fast. Commented May 22, 2018 at 14:36
7

Check your path. Referring a non-existant path or a very slow network share can cause the symptoms you are describing.

7

Below are 3 possible causes which I encountered. Of course, other problems not mentioned can also cause slow shell commands in Cygwin.

  • If you have the option "Automatically detect settings" in "LAN Settings", Windows will use the WPAD protocol to discover the local HTTP proxy. First it will send a DHCP "Inform" request with option 252, then it will try a DNS lookup on "wpad". These 2 operations can take a few seconds to time-out.

  • If the shell accesses some paths like /cygdrive/... , a NetBIOS name query will be executed, which can also take some time to time out.

  • If the shell accesses some paths like //mypath/... , a NetBIOS name query will be executed, which can also take some time to time out.

Solutions :

  • Disable "Automatically detect settings" in "LAN Settings" in the Windows "Internet Options" control panel.

  • Add the following entry in %SystemRoot%\system32\drivers\etc\hosts :

127.0.0.1 localhost cygdrive wpad

  • Make sure to avoid double slashes at the beginning of all paths.
4
  • "Disable "Automatically detect settings" in "LAN Settings"." - How do I do this ? I can't find the cygwin installer.
    – Steam
    Commented Jan 1, 2015 at 0:34
  • 1
    That is not a Cygwin setting, but rather a Windows setting
    – Demi
    Commented Jan 10, 2015 at 2:44
  • 1
    @Steam, search for Internet Options from Start > Search, open it, click Connections tab, click Settings, and uncheck Automatically detect settings Commented Jul 28, 2016 at 20:33
  • Right on. Thanks man, been looking for a while for a solution to slow directory changes on network UNC paths. To be a bit clearer, if you are browsing \\somehost\some\path, then somehost should be in the hosts file. This is especially helpful when using DFS mapped UNC paths. Commented Oct 18, 2018 at 20:53
6

Finally, I found the source - a service named "QQPCMgr RTP Service" running on my office computer, it's the real time protection service of "QQ PC Manager".

By disabling it, the time of the script in the question falls back to:

real    0m0.943s
user    0m0.105s
sys     0m0.231s

I have told the developers of QQPCMgr about this, hope they will find the reason.

This still much slower than Linux, but gets the same "real time" of other cygwin computers.

Thank you all!

1
  • 7
    How did you determine this? I looked for the service, but don't see it and I'm having the same issue. time for i in {1..10} ; do bash -c "echo Hello" ; done Hello ... real 0m18.070s user 0m0.061s sys 0m0.385s Commented Apr 17, 2013 at 22:13
2

This is how this problem manifested for me. For the first say 20 commands entered at the cygwin prompt it is fast, then it abruptly becomes painfully slow.

I checked that every item in my path was a valid directory.

I tried an authentication fix suggested elsewhere - https://superuser.com/questions/877051/cygwin-from-windows-very-slow-laggy/1247517 . It didn't work for me.

I replaced my exisitng virus scanner (System Center Endpoint Protection) with Sophos. That fixed the problem for me. Hope this helps.

1

I voted up on James McLeod, because starting a bash process takes some time, but it doesn't mean it will run commands slower than in UNIX.

Invoking bash -c from within a bash script is near to senseless, and Makefiles can call a lot of bash subprocesses, unless you append ; \ at the end of the commands.

For example, if a Makefile has the following:

echo Hello World
echo Good Bye

It will call two bash processes. To make it faster and call just one bash process:

echo Hello World; \
echo Good Bye

Debian has adopted dash instead of bash as the main shell, because starting many init scripts using bash will make the system take much longer to boot (each script call its own bash process).

1

Let's put some numbers to it. I ran the following:

time for i in {1..1000} ; do echo "Hello" ; done

The result I get from a standard Cygwin bash window is:

...
Hello
Hello

real    0m0.584s
user    0m0.031s
sys     0m0.000s

And from a xterm bash window on the same system I get:

...
Hello
Hello

real    0m0.037s
user    0m0.016s
sys     0m0.000s

I think this pretty much answers the question for you. The problem is you're going through a "Windows" "cmd" like window, which is inherently slow. Cygwin itself isn't the problem, it's the display trying to keep up that is slowing things down (for this test).

3
  • code $ time for i in {1..10} ; do bash -c "echo Hello" ; done Hello ... real 0m7.711s user 0m0.091s sys 0m0.476s
    – Lcsky
    Commented Jul 25, 2011 at 4:07
  • OK, something is definitely wrong with your setup if it's taking 700ms to execute a single echo. Did you try it in xterm? You have to download the X packages too to get that.
    – Jared
    Commented Jul 25, 2011 at 18:27
  • 2
    You're doing echo in the current shell whereas the op is spawning a new shell to do each echo. That's going to have a performance difference.
    – Burhan Ali
    Commented Nov 15, 2012 at 19:24
1

I was able to fix this problem by uninstalling bash-completion and switching from Kaspersky to Windows Security Essentials. (I diagnosed the Kaspersky interference using Process Explorer while running the "echo Hello" benchmark.) Brought my benchmark time from 7 seconds to 0.2.

1
1

On Comodo Internet Security Premium 10, i added "C:\Users\\Documents\MobaXterm\slash\bin" to the list of trusted files. (Settings -> File rating -> File list). Local terminal is fast again now.

1

For Windows Defender:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender]
"DisableAntiSpyware"=dword:00000001

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender\Real-Time Protection]
"DisableBehaviorMonitoring"=dword:00000001
"DisableOnAccessProtection"=dword:00000001
"DisableScanOnRealtimeEnable"=dword:00000001

or less dramatically:

powershell -Command Add-MpPreference -ExclusionPath "C:\opt\cygwin64"
powershell -Command Add-MpPreference -ExclusionPath "C:\home"
powershell -Command Add-MpPreference -ExclusionProcess "git.exe"
powershell -Command Add-MpPreference -ExclusionExtension ".c"
powershell -Command Add-MpPreference -ExclusionExtension ".cpp"
powershell -Command Add-MpPreference -ExclusionExtension ".cxx"

For Sophos:

REG ADD "HKLM\SYSTEM\CurrentControlSet\Services\Sophos Endpoint Defense\Scanning\Config" /v "OnAccessExcludeFilePaths" /t REG_MULTI_SZ /d "C:\Program Files (x86)\Sophos\Sophos Anti-Virus\\0C:\home\\0D:\Video\\" /f
REG ADD "HKLM\SYSTEM\CurrentControlSet\Services\Sophos Endpoint Defense\Scanning\Config" /v "OnAccessExcludeProcessPaths" /t REG_MULTI_SZ /d "C:\opt\cygwin64\\" /f
0

Invoking bash from a shell script can't be fast. Is it faster to just use

while true; echo hello; done

?

1
  • 5
    yeah, it's the reason why I added "bash -c" in the question line, if not, it runs very quickly! But the configure scripts, Makefiles run by invoking a new bash, and so it's very slowly.
    – Lcsky
    Commented Jul 25, 2011 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.