A simple example of JAVA serialization Performance

Source: Internet
Author: User
Let's take a look at a simple example of JAVA serialization performance-Linux general technology-Linux programming and kernel information. The following is a detailed description. My article EJB tells me how to fall in love with you-commenting on the excellent EJB and EJB, the performance of JAVA serialization is very low, at least the performance of JDK provided by SUN is very low, below is a simple test program to prove my statement:

First construct a simple test class

Class Class1 implements Serializable
{
Int m_nInt = 1;
Long m_nLong = 2;
Double m_dDouble = 3;
String m_sString = "test ";

Int getInt ()
{
Return m_nInt;
}
Void setInt (int nInt)
{
M_nInt = nInt;
}
Long getLong ()
{
Return m_nLong;
}
Void setLong (long nLong)
{
M_nLong = nLong;
}
Double getDouble ()
{
Return m_dDouble;
}
Void setDouble (double dDouble)
{
M_dDouble = dDouble;
}
String getString ()
{
Return m_sString;
}
Void setString (String sString)
{
M_sString = sString;
}
Public String toXMLString ()
{
Return "" + getInt () + "" +
"" + GetLong () + "" +
"" + GetDouble () + "" +
"" + GetString () + "" +
"";
}
}


In this class, I also wrote a toXMLString method. You can see that this is a very simple method to package data into XML strings.

Then write a serialization method to test the performance of the JAVA serialization method and the toXMLString method written by myself.

Public void testSerilize ()
Throws Exception
{
Class1 obj = new Class1 ();

Long lStartTime = System. currentTimeMillis ();
For (int I = 0; I <100000; I ++)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
ObjectOutputStream oos = new ObjectOutputStream (baos );
Oos. writeObject (obj );
Byte [] data = baos. toByteArray ();
}
System. out. println ("serialize used time:" + (System. currentTimeMillis ()-lStartTime ));


LStartTime = System. currentTimeMillis ();
For (int I = 0; I <100000; I ++)
{
Byte [] data = obj. toXMLString (). getBytes ();
}
System. out. println ("toxmlstring used time:" + (System. currentTimeMillis ()-lStartTime ));

}


From the code, we can see that after toXMLString is converted to String, a conversion to byte [] is also performed. Actually, two conversions are performed. This is a very poor packaging algorithm, there is still a lot of room for optimization. Ignore this and check the program running results:

First time:

Serialize used time: 6710

Toxmlstring used time: 3145


Second:

Serialize used time: 7081

Toxmlstring used time: 2974


Third time

Serialize used time: 6680

Toxmlstring used time: 3054

Have you seen this? The serialization method is more than twice as long as the toXmlString () method. The performance is poor. For RMI and EJB that fully use the serialization method, their performance can be imagined.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.