主题:【倡议】Java SE 5.0和.NET 2.0擂台赛 -- Highway
串行化/反串行化就是所谓的Serializtion/De-serailization。在现在计算机技术中这是非常重要的一个环节。一个Object只有被串行化才可以在网络上传输,才可以保存到Disk或Database中。
串行化/反串行化根据用户的要求可能会非常的复杂(比如Versioning control, Thread status, Delegate等等东西),Java和.NET都有专门的文章介绍。今天我想测试的就是Out-of-Box性能,就是看看最基本的串行化/反串行化这两者性能如何。
最基础的串行化/反串行化程序员不用写程序,只要声明一下就可以了(详见Code)。我的实验非常简单,创建一些Object,然后放到ArrayList里去,一次性串行化/反串行化,看看其性能如何。
测试表明,.NET程序反应很快,对于小数据,少次数的调用性能很好(比Java好),但是一旦数据量变大,性能马上就开始下降。这时候Java会迎头赶上。在数据量达到3MB的时候,Java已经快出5倍以上,而在30MB的时候,会快出60倍。Java Server VM在数据量大的情况下比Client有较大优势。如果是非密集型的运算,Client VM可能更合适。
[提示:原图站已失效]
[提示:原图站已失效]
(50000*10用的时间太长了,没有等它完成)
下面是我的源程序,非常简单。大家可以随意修改,玩弄!!!
C# Code
#region Using directives using System; using System.Collections; using System.Text; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; #endregion namespace SerialTest { class Program { static void Main(string[] args) { new Program(); } public Program() { ArrayList a50 = prepareData(50); seDesTest(a50,1000); ArrayList a500 = prepareData(500); seDesTest(a500,100); ArrayList a5000 = prepareData(5000); seDesTest(a5000,10); ArrayList a50000 = prepareData(50000); seDesTest(a50000,1); //ArrayList a100000 = prepareData(100000); //seDesTest(a100000); } private ArrayList prepareData(int num) { long start = System.Environment.TickCount; ArrayList a = new ArrayList(); for(int i=0; i<num; i++) { MyObj aObj = new MyObj(DateTime.Now.ToString(),32,i*1000,DateTime.Now); a.Add(aObj); } long time = System.Environment.TickCount- start; //System.Console.WriteLine("Prepare time of " + num + " items: " + time+ " ms."); return a; } private void seDesTest(ArrayList a, int number) { long start = System.Environment.TickCount; ArrayList a1 = null; for (int i = 0; i < number; i++) { MemoryStream memStream = new MemoryStream(); IFormatter formatter = new BinaryFormatter(); formatter.Serialize(memStream, a); byte[] data = memStream.ToArray(); memStream = new MemoryStream(data); a1 = (ArrayList)formatter.Deserialize(memStream); } long time = System.Environment.TickCount - start; if(a1.Count != a.Count) { System.Console.WriteLine("Error happend, Invalid test!!!"); return; } else { System.Console.WriteLine("Serialization and Deserialization of " + a.Count.ToString() + "*" + number.ToString() + " items take:" + time + " ms."); //System.Console.WriteLine("Last Object: " + a1[a1.Count - 1].ToString()); } } } [Serializable] public class MyObj { private static readonly String NEWLINE = "\r\n"; private String m_name; private int m_age; private double m_salary; private DateTime m_hireDate; /** Creates a new instance of MyObj */ public MyObj(String name, int age, double salary, DateTime hireDate) { m_name = name; m_age = age; m_salary = salary; m_hireDate = hireDate; } public MyObj() { } public override String ToString() { StringBuilder sb = new StringBuilder(); sb.Append("Name: " + m_name + NEWLINE); sb.Append("Age: " + m_age + NEWLINE); sb.Append("Salary: " + m_salary + NEWLINE); sb.Append("Hire Date: " + m_hireDate.ToString()); return sb.ToString(); } } }
Java Code
import java.util.*; import java.io.*; public class Main { public Main() throws Exception{ ArrayList a50 = prepareData(50); seDesTest(a50,1000); ArrayList a500 = prepareData(500); seDesTest(a500,100); ArrayList a5000 = prepareData(5000); seDesTest(a5000,10); ArrayList a50000 = prepareData(50000); seDesTest(a50000,1); seDesTest(a50000,10); //ArrayList a100000 = prepareData(100000); //seDesTest(a100000,1); } private ArrayList prepareData(int num){ long start = System.currentTimeMillis(); ArrayList a = new ArrayList(); for(int i=0; i<num; i++) { MyObj aObj = new MyObj(new Date().toString(),32,i*1000,new Date()); a.add(aObj); } long time = System.currentTimeMillis()-start; //System.out.println("Prepare time of " + num + " items: " + time+ " ms."); return a; } private void seDesTest(ArrayList a, int number)throws Exception{ long start = System.currentTimeMillis(); ArrayList a1 = null; for(int i=0; i<number; i++){ ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream(); ObjectOutputStream objOut = new java.io.ObjectOutputStream(bout); objOut.writeObject(a); byte[] data = bout.toByteArray(); bout.close(); objOut.close(); //deserializtion ByteArrayInputStream bin = new ByteArrayInputStream(data); ObjectInputStream objIn = new ObjectInputStream(bin); a1 = (ArrayList)objIn.readObject(); bin.close(); objIn.close(); } long time = System.currentTimeMillis()-start; if(a1.size() != a.size()){ System.out.println("Error happend, Invalid test!!!"); return; } else{ System.out.println("Serialization and Deserialization of " + a.size() + "*" + number +" items take:" + time + " ms."); } } public static void main(String[] args)throws Exception { // TODO code application logic here new Main(); } } import java.util.Date; import java.io.Serializable; public class MyObj implements Serializable{ private static final String NEWLINE = "\r\n"; private String m_name; private int m_age; private double m_salary; private Date m_hireDate; /** Creates a new instance of MyObj */ public MyObj(String name, int age, double salary, Date hireDate){ m_name = name; m_age = age; m_salary = salary; m_hireDate = hireDate; } public MyObj(){} public String toString() { StringBuffer sb = new StringBuffer(); sb.append("Name: " + m_name + NEWLINE); sb.append("Age: " + m_age + NEWLINE); sb.append("Salary: " + m_salary + NEWLINE); sb.append("Hire Date: " + m_hireDate.toString()); return sb.toString(); } }
- 相关回复 上下关系8
😉你试着再加个-Xms256M,看看结果如何? 老兵帅客 字808 2004-10-05 12:03:36
用不同的参数,运行多次,其结果比较参差。 Highway 字1401 2004-10-05 12:28:40
后天我才有时间对你的这个和下面那个问题进行测试 老兵帅客 字58 2004-10-04 15:19:10
😅串行化/反串行化小测试中Java取得绝对性胜利!
一件有趣的事情 老兵帅客 字1349 2004-10-07 21:43:31
有些技术,由于设计思想就不同,所以结果就会差别很大。比如 Highway 字289 2004-10-08 06:53:02
☹️这个我试验过了,用的是微软自己的例子,结果也一样 老兵帅客 字0 2004-10-05 12:06:07
我觉得你可以试着调整一下.Net的ArrayList的构造参数 老兵帅客 字246 2004-10-01 16:55:56