這是我的C#代碼。WebClient查詢需要很長時間才能完成,爲什麼?
WebClient client = new WebClient();
while (true) {
html = client.DownloadString("http://google.com");
Console.WriteLine(string.Format("\tSize: {0}", html.Length));
}
大約需要9秒鐘才能得到第一個結果。然後每個需要大約3秒鐘。
當我用Java來完成時,它需要不到1秒的時間。
你爲什麼覺得C#這麼慢?我該如何改進它?
您的網絡有問題。這在我的機器上需要0.16秒:System.Net.WebClient client = new System.Net.WebClient(); var sw = System.Diagnostics.Stopwatch.StartNew(); string html = client.DownloadString(「http://google.com」); Console.WriteLine(string.Format(「Size:{0}」,html.Length)); Console.WriteLine(「Elapsed:」+ sw.Elapsed); – Marek 2011-06-07 08:00:39
谷歌會扼殺你的請求。 – jgauffin 2011-06-07 08:01:26
@jgauffin:爲什麼? – Stefan 2011-06-07 08:02:34