Linq 性能测试

      Linq 提供了一种在内存中操作数据的高效方式,写了一个测试 比较生成同等xml 的时间开销.

   

  构建基础类 提供原始xml 操作 和 基于 linq 的xml 操作

  

using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;

namespace Common
{
    public class LinqHelper
    {
        //Generates XML using XmlDocument
        public static void GenerateXmlUsingXmlDocument()
        {
            MemoryStream ms = new MemoryStream();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "no"));
            XmlElement assembliesNode = xmlDoc.CreateElement("Assemblies");
            foreach (Type t in Assembly.GetAssembly(typeof(Object)).GetExportedTypes())
            {
                XmlElement assemblyNode = xmlDoc.CreateElement("Assembly");
                XmlAttribute fullTypeName = xmlDoc.CreateAttribute("FullTypeName");
                fullTypeName.Value = t.ToString();
                XmlAttribute isInterfaceName = xmlDoc.CreateAttribute("IsInterface");
                isInterfaceName.Value = t.IsInterface.ToString();
                assemblyNode.Attributes.Append(fullTypeName);
                assemblyNode.Attributes.Append(isInterfaceName);
                assembliesNode.AppendChild(assemblyNode);
            }
            xmlDoc.AppendChild(assembliesNode);
            xmlDoc.WriteContentTo(new XmlTextWriter(ms, System.Text.ASCIIEncoding.ASCII));
        }

        //Generates XML using XElement
        public static void GenerateXmlUsingXElement()
        {
            MemoryStream ms = new MemoryStream();
            XElement assembliesNode = new XElement("Assemblies",
                    from Type t in Assembly.GetAssembly(typeof(Object)).GetExportedTypes()
                    select new XElement("Assembly",
                        new XAttribute("FullTypeName", t.ToString()),
                        new XAttribute("IsInterface", t.IsInterface.ToString())));

            assembliesNode.Save(new XmlTextWriter(ms, System.Text.ASCIIEncoding.ASCII));
        }
    }
}

 

调用主题

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;
namespace XmlDocument_VS_XDocment
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            for (int index = 0; index < 51; index++)
            {
                sw.Reset(); sw.Start();
                LinqHelper.GenerateXmlUsingXmlDocument();
                sw.Stop();
                Console.Write("Generation time using XmlDocument " +
                    "and XElement: " + sw.ElapsedMilliseconds);
                sw.Reset(); sw.Start();
                LinqHelper.GenerateXmlUsingXElement();
                sw.Stop();
                Console.WriteLine(" : " + sw.ElapsedMilliseconds);
                GC.Collect();
            }
            Console.ReadKey();
        }
    }
}

 

控制台内执行结果

 

对比执行时间差异,结论已经很明确了.

 

转载于:https://www.cnblogs.com/QuickTechnology/p/5655304.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值