开发者

Why does Cygwin execute shell commands very slowly?

开发者 https://www.devze.com 2023-03-22 10:40 出处:网络
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 k开发者_如何学JAVAnow why?

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 k开发者_如何学JAVAnow 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


How about excluding Cygwin paths from your antivirus software ?


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


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.


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!


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.


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).


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).


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.


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.


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


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

while true; echo hello; done

?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号