ProtoStuff&&XStream性能相差30%

package foo;

import java.util.Date;
import java.util.List;

import com.dyuproject.protostuff.Tag;

public class Person {
	@Tag(alias="username", value = 1)
	private  String name;
	@Tag(alias="Motto", value = 2)
	private  String motto ;
	@Tag(alias="gender", value = 3)
	private  Gender gender = Gender.MALE;	
	@Tag(alias="userlist1", value = 4)
	private List<User> userlist ;
	@Tag(alias="date", value = 5)
	private  Date date ;
	
	enum Gender {  
		MALE(1) ,    FEMALE(2);
		private Gender(int value){
			this.value = value ;
		}		
		private int value;		
		public String toString(){
			 return super.toString()+"("+value+")";  
		}
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMotto() {
		return motto;
	}
	public void setMotto(String motto) {
		this.motto = motto;
	}
	public Gender getGender() {
		return gender;
	}
	public void setGender(Gender gender) {
		this.gender = gender;
	}
	public List<User> getUserlist() {
		return userlist;
	}
	public void setUserlist(List<User> userlist) {
		this.userlist = userlist;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	
}

 

package foo;


public class User {
	private String firstName;
	private String lastName;
	private String email;
//	private List<User> friends;

	public User() {
	}

	public User(String email) {
		this.email = email;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

//	public List<User> getFriends() {
//		return friends;
//	}
//
//	public void setFriends(List<User> friends) {
//		this.friends = friends;
//	}
}

 

package foo;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.XmlIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;

import foo.Person.Gender;

public class PSTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Person person = new Person();
		person.setName("John Doe");
		person.setMotto("Speed kills!");
		person.setGender(Gender.MALE);
		person.setDate(new Date());
		
		List<User> userlist = new ArrayList<User>();
		for(int i =0 ;i<10; i++){
			User user = new User();
			user.setEmail("cndone@gmail"+i);
			user.setFirstName("bie"+i);
			user.setLastName("哈哈"+i);
			userlist.add(user);
		}
		person.setUserlist(userlist);
		
		long begin = System.nanoTime();
		Schema<Person> schema = RuntimeSchema.getSchema(Person.class);  
		 byte[] xml = XmlIOUtil.toByteArray(person, schema);
		 String str = "";
		try {
			str = new String(xml, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 System.out.println(str);
		 long mid = System.nanoTime(); 
		 System.out.println(mid-begin);
		 
		 Person f = new Person();  
		 XmlIOUtil.mergeFrom(xml, f, schema);
		 System.out.println(f.getName());
		 System.out.println( new SimpleDateFormat("yyyy-MM-dd").format(f.getDate()));
		 long end = System.nanoTime(); 
		 System.out.println(end-mid);
		 
		 
	}

}

 

package foo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import foo.Person.Gender;

public class XStreamTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {		
		Person person = new Person();
		person.setName("John Doe");
		person.setMotto("Speed kills!");
		person.setGender(Gender.MALE);
		person.setDate(new Date());
		
		List<User> userlist = new ArrayList<User>();
		for(int i =0 ;i<10; i++){
			User user = new User();
			user.setEmail("cndone@gmail"+i);
			user.setFirstName("bie"+i);
			user.setLastName("哈哈"+i);
			userlist.add(user);
		}
		person.setUserlist(userlist);
		
		/**bean 2 XML*/
		long begin = System.nanoTime();
		XStream xstream = new XStream(new DomDriver());
		xstream.alias("Person", Person.class);
		xstream.alias("User", User.class);
		String str = xstream.toXML(person);
		 System.out.println(str);
		 long mid = System.nanoTime(); 
		 System.out.println(mid-begin);
		 
		 /**XML 2 BEAN*/
		 Person f = (Person) xstream.fromXML(str);
		 System.out.println(f.getName());
		 System.out.println( new SimpleDateFormat("yyyy-MM-dd").format(f.getDate()));
		 long end = System.nanoTime(); 
		 System.out.println(end-mid);
		 
	}

}

 测试结果:

<!--StartFragment -->
 
参考:

你可能感兴趣的:(xstream)