测试了一把 XslTransform 的性能
代码如下:
Using directives
namespace testXsltTransform
{
class Program
{
static string xml0, xslt0, prefix;
definition
public Program(string _prefix, string xml, string xslt)
{
prefix = _prefix;
xml0 = xml;
xslt0 = xslt;
}
public interface IXmlLoader
{
XPathNavigator GetXml();
}
public interface ITransformLoader
{
XslTransform GetTransform();
}
XPath Loader
XmlDocument Loader
XmlDataDocument Loader
cache
void trans(IXmlLoader xml, ITransformLoader trans)
{
//using (MemoryStream output = new MemoryStream())
using(NullStream output = new NullStream())
{
trans.GetTransform().Transform(xml.GetXml(), null, output);
output.Flush();
//Console.WriteLine(Encoding.Default.GetString(output.ToArray()));
}
}
public class NullStream : System.IO.Stream
{
public override bool CanRead{get { return false; }}
public override bool CanSeek{get { return false; }}
public override bool CanWrite{get { return true; }}
public override void Flush(){}
public override long Length{get { throw new global::System.NotImplementedException(); }}
public override long Position
{
get{throw new global::System.NotImplementedException();}
set{throw new global::System.NotImplementedException();}
}
public override int Read(byte[] buffer, int offset, int count){throw new NotImplementedException();}
public override long Seek(long offset, SeekOrigin origin){throw new NotImplementedException();}
public override void SetLength(long value){throw new NotImplementedException();}
public override void Write(byte[] buffer, int offset, int count){}
}
public class Tri
{
public string descr;
public IXmlLoader xml;
public ITransformLoader trans;
public Tri(string _des, IXmlLoader _xml, ITransformLoader _trans)
{
descr = _des; xml = _xml; trans = _trans;
}
}
testcode
static void Main(string[] args)
{
Program prog = new Program("xslt1 ", const_xml0, const_xslt0);
prog.doTest();
prog = new Program("xslt2 ", const_xml0, const_xslt1);
prog.doTest();
}
/*
Console Display:
round 0
round 1
round 2
round 3
round 4
round 5
result:
xslt1 doc: no cache 220 210 240 220 210
xslt1 xpath: no cache 210 190 180 180 190
xslt1 datadoc: no cache 370 360 360 350 360
xslt1 doc: doc cache 180 190 190 200 190
xslt1 xpath: doc cache 150 150 150 160 150
xslt1 datadoc: doc cache 620 640 640 620 610
xslt1 doc: xslt cache 90 90 70 90 80
xslt1 xpath: xslt cache 80 70 80 70 80
xslt1 datadoc: xslt cache 180 190 180 180 180
xslt1 doc: all cache 50 50 50 60 50
xslt1 xpath: all cache 40 50 40 40 40
xslt1 datadoc: all cache 430 440 420 450 410
round 0
round 1
round 2
round 3
round 4
round 5
result:
xslt2 doc: no cache 220 200 220 230 220
xslt2 xpath: no cache 160 250 170 170 160
xslt2 datadoc: no cache 360 390 360 350 370
xslt2 doc: doc cache 170 180 180 170 170
xslt2 xpath: doc cache 150 140 140 150 160
xslt2 datadoc: doc cache 630 650 711 701 761
xslt2 doc: xslt cache 80 80 80 80 80
xslt2 xpath: xslt cache 140 70 80 70 70
xslt2 datadoc: xslt cache 200 180 170 180 180
xslt2 doc: all cache 80 50 50 40 50
xslt2 xpath: all cache 50 40 40 40 30
xslt2 datadoc: all cache 550 430 440 450 450
*/
}
}
namespace testXsltTransform
{
class Program
{
static string xml0, xslt0, prefix;
definition
public Program(string _prefix, string xml, string xslt)
{
prefix = _prefix;
xml0 = xml;
xslt0 = xslt;
}
public interface IXmlLoader
{
XPathNavigator GetXml();
}
public interface ITransformLoader
{
XslTransform GetTransform();
}
XPath Loader
XmlDocument Loader
XmlDataDocument Loader
cache
void trans(IXmlLoader xml, ITransformLoader trans)
{
//using (MemoryStream output = new MemoryStream())
using(NullStream output = new NullStream())
{
trans.GetTransform().Transform(xml.GetXml(), null, output);
output.Flush();
//Console.WriteLine(Encoding.Default.GetString(output.ToArray()));
}
}
public class NullStream : System.IO.Stream
{
public override bool CanRead{get { return false; }}
public override bool CanSeek{get { return false; }}
public override bool CanWrite{get { return true; }}
public override void Flush(){}
public override long Length{get { throw new global::System.NotImplementedException(); }}
public override long Position
{
get{throw new global::System.NotImplementedException();}
set{throw new global::System.NotImplementedException();}
}
public override int Read(byte[] buffer, int offset, int count){throw new NotImplementedException();}
public override long Seek(long offset, SeekOrigin origin){throw new NotImplementedException();}
public override void SetLength(long value){throw new NotImplementedException();}
public override void Write(byte[] buffer, int offset, int count){}
}
public class Tri
{
public string descr;
public IXmlLoader xml;
public ITransformLoader trans;
public Tri(string _des, IXmlLoader _xml, ITransformLoader _trans)
{
descr = _des; xml = _xml; trans = _trans;
}
}
testcode
static void Main(string[] args)
{
Program prog = new Program("xslt1 ", const_xml0, const_xslt0);
prog.doTest();
prog = new Program("xslt2 ", const_xml0, const_xslt1);
prog.doTest();
}
/*
Console Display:
round 0
round 1
round 2
round 3
round 4
round 5
result:
xslt1 doc: no cache 220 210 240 220 210
xslt1 xpath: no cache 210 190 180 180 190
xslt1 datadoc: no cache 370 360 360 350 360
xslt1 doc: doc cache 180 190 190 200 190
xslt1 xpath: doc cache 150 150 150 160 150
xslt1 datadoc: doc cache 620 640 640 620 610
xslt1 doc: xslt cache 90 90 70 90 80
xslt1 xpath: xslt cache 80 70 80 70 80
xslt1 datadoc: xslt cache 180 190 180 180 180
xslt1 doc: all cache 50 50 50 60 50
xslt1 xpath: all cache 40 50 40 40 40
xslt1 datadoc: all cache 430 440 420 450 410
round 0
round 1
round 2
round 3
round 4
round 5
result:
xslt2 doc: no cache 220 200 220 230 220
xslt2 xpath: no cache 160 250 170 170 160
xslt2 datadoc: no cache 360 390 360 350 370
xslt2 doc: doc cache 170 180 180 170 170
xslt2 xpath: doc cache 150 140 140 150 160
xslt2 datadoc: doc cache 630 650 711 701 761
xslt2 doc: xslt cache 80 80 80 80 80
xslt2 xpath: xslt cache 140 70 80 70 70
xslt2 datadoc: xslt cache 200 180 170 180 180
xslt2 doc: all cache 80 50 50 40 50
xslt2 xpath: all cache 50 40 40 40 30
xslt2 datadoc: all cache 550 430 440 450 450
*/
}
}
我的机器配置如下:赛扬900,512M内存。编译采用VS C# Express 2005
开始时候的测试循环次数是10次,结果发现太小,分辨不开,结果采用1000次循环,结果出现了很多性能异常情况,分析结果是循环次数太多导致内存耗用太大,影响评测,结果用循环100次得到这个结果,基本满意。
从结果中我们可以看到,XPathDocument支持的xslt转换速度始终最快,无论xslt中是否包含xpath引用。其次是XmlDocument支持的转换,最后才是XmlDataDocument。
而在优化方面,预先装入XML而运行时装入xslt要比预先装入XSLT而运行时装入xml的测试速度要慢差不多一个级别,说明系统装入xslt的耗费远比装入xml的耗费要大得多。大家以后一定要注意,不要经常去 new XslTransform() ,一定要预先装载好xslt。
另外需要注意的是,使用XmlDataDocument支持的转换,即使在全部cache的情况下,也和其他两种方法全部没有cache的情况一样速度。而且和最快方案的速度差距竟然有6倍之多!!!
最后,尝试了好几下去上传图片,结果都没有成功。怎么回事啊?图片17k,300x400点阵的gif。谁帮我一下?
公众号:老翅寒暑