序列化和反序列化相同的对象Simple数量10000
kryo序列化的时间 : 99ms
kryo反序列化的时间 : 41ms
java原生的序列化时间 : 303ms
java原生的发序列化时间 : 402ms
package com.hualala.serialize;
import java.io.Serializable;
import java.util.Map;
/**
* @author hzw
* @date 2018/7/12 12:10
* @Description: ${todo}
*/
public class Simple implements Serializable {
private static final long serialVersionUID = 9199352754030969128L;
private String name;
private int age;
private Map<String, Integer> map;
public Simple() {}
public Simple(String name, int age, Map<String, Integer> map) {
this.name = name;
this.age = age;
this.map = map;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Map<String, Integer> getMap() {
return map;
}
public void setMap(Map<String, Integer> map) {
this.map = map;
}
}
package com.hualala.serialize;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import org.objenesis.strategy.StdInstantiatorStrategy;
/**
* @author hzw
* @date 2018/7/12 14:25
* @Description: ${todo}
*/
public class KyroSerialize {
public static void main(String[] args) throws FileNotFoundException {
long statr = System.currentTimeMillis();
setSerializeObject();
System.out.println("kryo序列化的时间 : "+(System.currentTimeMillis()-statr)+"ms");
statr = System.currentTimeMillis();
getSerializeObject();
System.out.println("kryo反序列化的时间 : "+(System.currentTimeMillis()-statr)+"ms");
}
public static void setSerializeObject() throws FileNotFoundException {
Kryo kryo = new Kryo();
kryo.setReferences(false);
kryo.setRegistrationRequired(false);
kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
Output output = new Output(new FileOutputStream("F:\\mytest\\simplekryo.bin"));
for (int i = 0; i < 10000; i++) {
HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
stringIntegerHashMap.put("huangzhiwei01", i+1);
stringIntegerHashMap.put("huangzhiwei02", i+1);
kryo.writeObject(output, new Simple("ddd", 10+i, stringIntegerHashMap));
}
output.flush();
output.close();
}
public static void getSerializeObject() throws FileNotFoundException {
Kryo kryo = new Kryo();
kryo.setReferences(false);
kryo.setRegistrationRequired(false);
kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
Input input = new Input(new FileInputStream("F:\\mytest\\simplekryo.bin"));
Simple simple = null;
try {
while ((simple=kryo.readObject(input,Simple.class)) != null){
}
}catch (KryoException e){
}
input.close();
}
}
package com.hualala.serialize;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
/**
* @author hzw
* @date 2018/7/12 13:54
* @Description: ${todo}
*/
public class OriginalSerialize {
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
setSerializzeObject();
System.out.println("java原生的序列化时间 : "+ (System.currentTimeMillis()-start)+"ms");
start = System.currentTimeMillis();
getSerializeObject();
System.out.println("java原生的发序列化时间 : "+(System.currentTimeMillis()-start)+"ms");
}
public static void setSerializzeObject() throws Exception {
FileOutputStream fileOutputStream = new FileOutputStream("F:\\mytest\\simple.bin");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
for (int i = 0; i < 10000; i++) {
HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
stringIntegerHashMap.put("huangzhiwei01", i);
stringIntegerHashMap.put("huangzhiwei02", i);
objectOutputStream.writeObject(new Simple("dada", i+10, stringIntegerHashMap));
}
objectOutputStream.flush();
objectOutputStream.close();
}
public static void getSerializeObject() throws IOException, ClassNotFoundException {
FileInputStream fileInputStream = new FileInputStream("F:\\mytest\\simple.bin");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Simple simple = null;
try {
while ((simple = (Simple)objectInputStream.readObject()) != null){
}
}catch (EOFException e){
}
objectInputStream.close();
}
}