Google Protocol Buffers和java字符串处理对比

操作代码大部分从晚上拷贝,懒得敲。

下面直接上有源码和测试结果。

serabuffer.proto文件,用下面命令生成java代码。

protoc -I=./ --java_out=./ serabuffer.proto


package Feinno.Practice.Learn;

option java_package = "Feinno.Practice.Learn";
option java_outer_classname = "ProtoBufferPractice";

message msgInfo  {
  required int32 ID = 1;
  required int64 GoodID = 2;
  required string Url = 3;
  required string Guid = 4;
  required string Type = 5;
  required int32 Order = 6;
}

下面是java部分代码,直接输出结果,要测试不同字符串长度的,可以直接替换其中的字符串然后run即可。

package Feinno.Practice.Learn;

import com.google.protobuf.ByteString;

public class Test
{
    public byte[] serialize()
    {
        ProtoBufferPractice.msgInfo.Builder builder=ProtoBufferPractice.msgInfo.newBuilder();  
        builder.setGoodID(100);  
        builder.setGuid("11111-23222-3333-444");  
        builder.setOrder(0);  
        builder.setType("及基于消息的协调机制不适合在某些应用中使用,因此需要有一种可靠的、可扩展的、分布式的、可配置的协调机制来统一系统的状态");  
        builder.setID(10);  
        builder.setUrl("http://www.gufensoso.com/search/?q=java+protocol+buffer");  
        ProtoBufferPractice.msgInfo info=builder.build();        
        byte[] result=info.toByteArray() ;  
        return result;
    }
    
    public void deserialize(ByteString result)
    {
        try{  
            ProtoBufferPractice.msgInfo msg = ProtoBufferPractice.msgInfo.parseFrom(result);  
//            System.out.println(msg);  
        }  
        catch(Exception ex){  
            System.out.println(ex.getMessage());  
        }
    }
    
    public static void main(String[] args) 
    {
        Test t=new Test();
        long s1=System.nanoTime();
        byte[] b = t.serialize();
        long s2=System.nanoTime();        
        System.out.println("序列化后长度是:"+b.length+" 耗时:"+(s2-s1)); 
        
        long a1=System.nanoTime();  
        t.deserialize(ByteString.copyFrom(b));
        long a2=System.nanoTime();  
        System.out.println("反序列化耗时:"+(a2-a1)); 
        
        long s3=System.nanoTime();        
        StringBuilder sb=new StringBuilder();
        sb.append("Ã").append("100").append("Ã").append("11111-23222-3333-444").append("Ã").append("0").append("Ã").append("及基于消息的协调机制不适合在某些应用中使用,因此需要有一种可靠的、可扩展的、分布式的、可配置的协调机制来统一系统的状态").append("Ã").append("10").append("Ã").append("http://xxx.jpg");
        byte[] c=sb.toString().getBytes();
        long s4=System.nanoTime();
        
        System.out.println("拼接字符后长度:"+c.length+" 耗时:"+(s4-s3));
        long c1=System.nanoTime(); 
        sb.toString().split("Ã");
        long c2=System.nanoTime(); 
        System.out.println("拆字符串耗时:"+(c2-c1)); 
        
        
    }
}

1、字符串长度较短时:
序列化后长度是:50 耗时:32115919
拼接字符后长度:56 耗时:36223
反序列化耗时:2484294
拆字符串耗时:305783

2、字符串长度较长时,也就是现在代码中的。
序列化后长度是:265 耗时:20092297
拼接字符后长度:229 耗时:48297
反序列化耗时:2445656
拆字符串耗时:354683

3、字符串长度再加长时
序列化后长度是:435 耗时:26542406
拼接字符后长度:398 耗时:53127
反序列化耗时:5412019
拆字符串耗时:527950

结果:
1、速度方面,拼接字符串速度是序列化的500-1000倍,拆分字符串是反序列化的8倍左右;
2、大小方面,数据小的序列化的体积小,数据大了以后,字符串拼接有优势;
3、上面结果数据其实是不固定的,和具体的数据有关。


结论:

网上的资料显示,protocol buffer在序列化方面不管提交还是性能方面都是很优秀的,但是这里的测试结果显示和字符串处理方式来比差距就太明显了。

不要迷信,需要自己测试过才有体会,有些场景,用字符串处理的方式比序列化的方式更有优势,消息中间件如果用字符串的方式来处理,性能上应该有成倍提升。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值