今天對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的代碼:url
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字符串序列化成對象列表。spa
GO每次的運行時間大體是12秒左右。xml
NET的運行時間大體是2秒左右。對象
能夠看出GO在XML的處理上仍是比NET慢了一個檔次,但願谷歌之後能優化這個功能。blog