1

我已经在我的应用程序中追踪到下面正在计时的代码位。我知道这将是一个缓慢的点,但每个请求平均需要 1 秒。我所追求的 xml 总是在第一个标签中,所以我不认为是下载时间让我感到困惑。

Stopwatch stopwatch = new Stopwatch();
XmlTextReader reader = new XmlTextReader("http://steamcommunity.com/id/test?xml=1");

stopwatch.Reset();
stopwatch.Start();

while (reader.Read()) {
 if (reader.Name.Equals("steamID64")) {
  reader.Read();

  stopwatch.Stop();

  time = stopwatch.ElapsedMilliseconds();
  return Convert.ToInt64(reader.Value);
 }
}

是否有更快的方法来读取我想要的标签,或者我是否受到我从中下载 xml 文件的服务器的限制?

谢谢。

4

4 回答 4

3

我认为您正在测量创建连接所需的时间。要确认这一点,您可以将 Reset+Start 行移至阅读器创建的上方。我预计会有很少或没有区别。

如果是连接时间,则取决于网络,并且您可以在代码中进行注释。也许您可以通过调整网络设置来获得一些改进。但那是另一个论坛。

于 2010-08-26T21:50:58.090 回答
1

尝试设置

reader.XmlResolver = null;
于 2010-08-26T21:52:39.510 回答
1

所以我不认为是下载时间让我

为了确认这一点,您是否考虑过在本地下载文件,然后查看时间是什么样的

于 2010-08-26T21:55:31.420 回答
1

我已经将您的方法与其他方法进行了比较。只需下载数据并通过正则表达式查找 id。

        System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
        stopwatch.Reset();
        stopwatch.Start();

        System.Net.WebClient wc = new System.Net.WebClient();
        string s = wc.DownloadString("http://steamcommunity.com/id/test?xml=1");
        System.Text.RegularExpressions.Regex re = new Regex("\\<steamID64\\>(\\d+)\\</steamID64\\>");
        System.Text.RegularExpressions.Match m = re.Match(s);
        if (m != null && m.Captures.Count != 0) Response.Write("steamID64: " + m.Captures[0].Value + " <br/>");
        stopwatch.Stop();

        long time = stopwatch.ElapsedMilliseconds;
        Response.Write("Time Elapsed (1):" + time.ToString() +" <br/>");

        stopwatch.Reset();
        stopwatch.Start();

        System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader("http://steamcommunity.com/id/test?xml=1");

        stopwatch.Reset();
        stopwatch.Start();

        while (reader.Read())
        {
            if (reader.Name.Equals("steamID64"))
            {
                reader.Read();
                stopwatch.Stop();

                time = stopwatch.ElapsedMilliseconds;
                s = reader.Value;
                break;
            }
        }

        Response.Write("<br/>steamID64: " + s );
        Response.Write("<br/>Time Elapsed (2):" + time.ToString() + " <br/>");

** 结果:

steamID64: 76561197991558078 经过时间 (1):1572

steamID64: 76561197991558078 经过时间 (2):969

XmlReader 更好:)。

于 2010-08-26T22:12:39.713 回答