这是我的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