今天对GO和NET的XML字符串序列化成对象列表做了一个性能比较,得出一些结论。
GO的代码:
package main
import (
"encoding/xml"
"io/ioutil"
"log"
)
type Result struct {
XMLName xml.Name `xml:"ArrayOfDoc"`
Persons []Person `xml:"doc"`
}
type Person struct {
Url string `xml:"url"`
Docno string `xml:"docno"`
Contenttitle string `xml:"contenttitle"`
Content string `xml:"content"`
}
func main() {
content, err := ioutil.ReadFile("E:\\Xml\\test2.xml")
if err != nil {
log.Fatal(err)
}
var result Result
timer2 := time.NewTimer(time.Second)
err = xml.Unmarshal(content, &result)
if err != nil {
log.Fatal(err)
}
log.Println(len(result.Persons))
}
NET的代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.XPath;
namespace ConsoleApplicationDataInsert
{
class Program
{
static void Main(string[] args)
{
IList<string> files = new List<string>();
files.Add(@"E:\Xml\test2.xml");
int count = 0;
Console.WriteLine(DateTime.Now.ToString() + " 开始解析");
foreach (var file in files)
{
string xml = File.ReadAllText(file);
Console.WriteLine(DateTime.Now.ToString() + " 读取完毕");
var listModel = Deserialize(typeof(List<doc>), xml) as List<doc>;
count += listModel.Count;
}
/*
List<doc> list = new List<doc>();
list.Add(new doc() { content = "abdcdsfds", contenttitle = "rewrewre", docno = "rtrwetrew", url = "rewrewrewrew" });
list.Add(new doc() { content = "abdcfewrwdsfds", contenttitle = "rewrewfdsare", docno = "rtrwetrfdsew", url = "rewrewrefdsfwrew" });
string xml2 = Serializer(typeof(List<doc>), list);
* */
Console.WriteLine(DateTime.Now.ToString() + " 解析完成,总共:" + count);
Console.Read();
}
static object Deserialize(Type type, string xml)
{
try
{
using (StringReader sr = new StringReader(xml))
{
XmlSerializer xmldes = new XmlSerializer(type);
return xmldes.Deserialize(sr);
}
}
catch (Exception e)
{
return null;
}
}
static string Serializer(Type type, object obj)
{
MemoryStream Stream = new MemoryStream();
XmlSerializer xml = new XmlSerializer(type);
try
{
//序列化对象
xml.Serialize(Stream, obj);
}
catch (InvalidOperationException)
{
throw;
}
Stream.Position = 0;
StreamReader sr = new StreamReader(Stream);
string str = sr.ReadToEnd();
sr.Dispose();
Stream.Dispose();
return str;
}
}
public class doc
{
public string url { get; set; }
public string docno { get; set; }
public string contenttitle { get; set; }
public string content { get; set; }
}
}
两者都是对一个170M(里面有70203个XML对象)的XML文件进行序列化,将XML字符串序列化成对象列表。
GO每次的运行时间大致是12秒左右。
NET的运行时间大致是2秒左右。
可以看出GO在XML的处理上还是比NET慢了一个档次,希望谷歌以后能优化这个功能。