I'm trying to get better performance of object's serialization by usage of kryo
library.
I have the next class
public class CustomClass {
private String name;
private int[] array;
public CustomClass(String name, int size){
this.name = name;
fillArray(size);
}
private CustomClass(){ }
private void fillArray(int size){
array = new int[size];
Random random = new Random();
for (int i = 0; i < size; i++){
array[i] = random.nextInt();
}
}
}
I'm serilizing it by this method, note I'm making serialization of single instance
public void kryoWrite(Object object){
Kryo kryo = new Kryo();
Output output = null;
try {
output = new Output(new FileOutputStream("kryo.txt"));
kryo.writeObject(output, object);
} catch (IOException e){
e.printStackTrace();
} finally {
if (output != null) {
output.close();
}
}
}
but serializing the same object using standard Java's Serializable
interface works faster.
For example, when I pass 1000000 as second param to constructor kryo
serialize object in 188 ms, when Serializable
serializes exactly the same object in 136 ms.
So what I'm doing wrong (is it a dubstep in my song, lol)?
EDIT
Serialization of array with size of 1000000, created and serializaed by these methods appropriately
public static int[] getArray(int size){
int[] array = new int[size];
Random random = new Random();
for (int i = 0; i < size; i++){
array[i] = random.nextInt();
}
return array;
}
public static void kryoWriteArray(int[] array) throws FileNotFoundException {
Kryo kryo = new Kryo();
Output output = new Output(new FileOutputStream("array.txt"));
output.writeInts(array);
output.close();
}
takes 139 ms.