protobuf和json字符串大小对比

json字符串拼装程序

package org.proto;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Test3 {

    public static void main(String[] args) {
        Random rand = new Random();

        List<MyPoint> list = new ArrayList<>();

        for(int i=1000000;i<1500000;i++){
            MyPoint myPoint = new MyPoint ();

            myPoint.setVin("M"+i);

            myPoint.setLng(11600000 + rand.nextInt(100000));
            myPoint.setLat(3900000 + rand.nextInt(100000));

            list.add(myPoint);
        }

        String str = JSON.toJSON(list).toString();

        System.out.println(str.length());
    }

    public static class MyPoint {
        private String vin;
        private int lng;
        private int lat;

        public String getVin() {
            return vin;
        }

        public void setVin(String vin) {
            this.vin = vin;
        }

        public int getLng() {
            return lng;
        }

        public void setLng(int lng) {
            this.lng = lng;
        }

        public int getLat() {
            return lat;
        }

        public void setLat(int lat) {
            this.lat = lat;
        }
    }
}

json字符串空间大小

protobuf和json字符串大小对比

 

 

protobuf的数据结构

option java_outer_classname = "TestProtoPoint2";
option java_package = "org.proto";

message Points{
    repeated MyPoint myPoint=1;
}

message MyPoint{
     optional string vin=1;
     optional int32 lng=2;
     optional int32 lat=3;
}

 

protobuf数据拼装程序

package org.proto;

import java.io.FileOutputStream;
import java.util.Random;

public class Test2 {

    public static void main(String[] args) throws Exception{

        final TestProtoPoint2.Points.Builder builder = TestProtoPoint2.Points.newBuilder();

        Random rand = new Random();

        for(int i=1000000;i<1500000;i++){
            TestProtoPoint2.MyPoint.Builder myPoint = TestProtoPoint2.MyPoint.newBuilder();

            myPoint.setVin("M"+i);

            myPoint.setLng(11600000 + rand.nextInt(100000));
            myPoint.setLat(3900000 + rand.nextInt(100000));

            builder.addMyPoint(myPoint);
        }

        byte[] bs = builder.build().toByteArray();

        FileOutputStream fos = new java.io.FileOutputStream("d:/test.pbf");

        fos.write(bs);
        fos.flush();
        fos.close();

        System.out.println(bs.length);
    }
}

 

protobuf序列号空间占用大小

protobuf和json字符串大小对比

 

 

上一篇:java 循环删除hashmap中的键值对,解决java.util.ConcurrentModificationException报错


下一篇:创建自定义线程池推荐ThreadPoolExecutor