Home Java javaTutorial Examples of performance comparison analysis between Java native serialization and Kryo serialization

Examples of performance comparison analysis between Java native serialization and Kryo serialization

Oct 10, 2017 am 10:25 AM
java kryo Serialization

This article mainly introduces the comparative analysis of Java native serialization and Kryo serialization performance examples, involving Java and kryo serialization and deserialization related examples. The editor thinks it is very good, and I share it with you here. I hope to give you a refer to.

Introduction

In recent years, various new efficient serialization methods have emerged in an endless stream, constantly refreshing the upper limit of serialization performance. The most typical ones include:

Specifically for Java language: Kryo, FST, etc.

Cross-language: Protostuff, ProtoBuf, Thrift, Avro, MsgPack, etc.

The performance of most of these serialization methods is significantly better than hessian2 (even including the immature dubbo serialization). In view of this, we introduced two efficient Java serialization implementations, Kryo and FST, for dubbo to gradually replace hessian2. Among them, Kryo is a very mature serialization implementation that has been widely used in Twitter, Groupon, Yahoo, and many well-known open source projects (such as Hive and Storm). FST is a newer serialization implementation that currently lacks enough mature use cases, but it is still very promising. Let’s compare the performance of java native serialization and Kryo serialization

1. Entity class Simple.java


package bhz.entity;
import java.io.Serializable;
import java.util.Map;
public class Simple implements Serializable
{ 
   private static final long serialVersionUID = -4914434736682797743L; 
   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; 
   } 
}
Copy after login

2. Java native serialization OriginalSerializable.java


package bhz.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import bhz.entity.Simple;
public class OriginalSerializable { 
  public static void main(String[] args) throws IOException, ClassNotFoundException { 
    long start = System.currentTimeMillis(); 
    setSerializableObject(); 
    System.out.println("java原生序列化时间:" + (System.currentTimeMillis() - start) + " ms" );  
    start = System.currentTimeMillis(); 
    getSerializableObject(); 
    System.out.println("java原生反序列化时间:" + (System.currentTimeMillis() - start) + " ms"); 
  } 
  public static void setSerializableObject() throws IOException{ 
    FileOutputStream fo = new FileOutputStream("D:/file2.bin"); 
    ObjectOutputStream so = new ObjectOutputStream(fo); 
    for (int i = 0; i < 100000; i++) { 
      Map<String,Integer> map = new HashMap<String, Integer>(2); 
      map.put("zhang0", i); 
      map.put("zhang1", i); 
      so.writeObject(new Simple("zhang"+i,(i+1),map)); 
    } 
    so.flush(); 
    so.close(); 
  } 
  public static void getSerializableObject(){ 
     FileInputStream fi; 
    try { 
      fi = new FileInputStream("D:/file2.bin"); 
      ObjectInputStream si = new ObjectInputStream(fi); 
      Simple simple =null; 
      while((simple=(Simple)si.readObject()) != null){ 
        //System.out.println(simple.getAge() + " " + simple.getName()); 
      } 
      fi.close(); 
      si.close(); 
    } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
    } catch (IOException e) { 
      //e.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
    } 
  } 
}
Copy after login

3. kyro serialization KyroSerializable.java


package bhz.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.objenesis.strategy.StdInstantiatorStrategy;
import bhz.entity.Simple;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
public class KyroSerializable { 
  public static void main(String[] args) throws IOException { 
    long start = System.currentTimeMillis(); 
    setSerializableObject(); 
    System.out.println("Kryo 序列化时间:" + (System.currentTimeMillis() - start) + " ms" ); 
    start = System.currentTimeMillis(); 
    getSerializableObject(); 
    System.out.println("Kryo 反序列化时间:" + (System.currentTimeMillis() - start) + " ms"); 
  } 
  public static void setSerializableObject() throws FileNotFoundException{ 
    Kryo kryo = new Kryo(); 
    kryo.setReferences(false); 
    kryo.setRegistrationRequired(false); 
    kryo.setInstantiatorStrategy(new StdInstantiatorStrategy()); 
    kryo.register(Simple.class); 
    Output output = new Output(new FileOutputStream("D:/file1.bin")); 
    for (int i = 0; i < 100000; i++) { 
      Map<String,Integer> map = new HashMap<String, Integer>(2); 
      map.put("zhang0", i); 
      map.put("zhang1", i); 
      kryo.writeObject(output, new Simple("zhang"+i,(i+1),map)); 
    } 
    output.flush(); 
    output.close(); 
  } 
  public static void getSerializableObject(){ 
    Kryo kryo = new Kryo(); 
    kryo.setReferences(false); 
    kryo.setRegistrationRequired(false); 
    kryo.setInstantiatorStrategy(new StdInstantiatorStrategy()); 
    Input input; 
    try { 
      input = new Input(new FileInputStream("D:/file1.bin")); 
      Simple simple =null; 
      while((simple=kryo.readObject(input, Simple.class)) != null){ 
        //System.out.println(simple.getAge() + " " + simple.getName() + " " + simple.getMap().toString()); 
      } 
      input.close(); 
    } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
    } catch(KryoException e){ 
    } 
  } 
}
Copy after login

4. Comparison of test results

java native serialization time: 8281 ms

java native deserialization time: 5899 ms

and

Kryo serialization time: 630 ms

Kryo deserialization time: 15 ms

After comparison, it can be found that kryo is more than ten times better than Java's native serialization performance

Summary

The above is the detailed content of Examples of performance comparison analysis between Java native serialization and Kryo serialization. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

See all articles