代码之家  ›  专栏  ›  技术社区  ›  Radu

XML读取器性能

  •  1
  • Radu  · 技术社区  · 14 年前

    我已经将我的应用程序中的惰性追溯到下面计时的代码位。我知道这将是一个缓慢的点,但每个请求平均需要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);
     }
    }
    

    4 回复  |  直到 14 年前
        1
  •  1
  •   Henk Holterman    14 年前

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

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

        2
  •  3
  •   Henk Holterman    14 年前

    我认为你在衡量建立联系所需的时间。要确认这一点,可以将Reset+Start行移到读卡器创建的上方。我预计会有很少或没有区别。

        3
  •  1
  •   Jonathan    14 年前

    尝试设置

    reader.XmlResolver = null;
    
        4
  •  1
  •   Zafer    14 年前

    我把你的方法和另一种方法作了比较。只需下载数据并通过正则表达式找到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/>");
    

    蒸汽ID64:76561197991558078 已用时间(1):1572

    蒸汽ID64:76561197991558078

    XmlReader更好:)。

    推荐文章