MessagePack serialization time is slower than Json

Asked 2 years ago, Updated 2 years ago, 124 views

I would like to use MessagePack to speed up API communication.
See how different serialization de-serialization speeds are
Using MessagePack and JSON,
We are comparing the speed using the following process.

■add
There was an error in the measurement, so the source code was rewritten and the measurement value was re-measured
Add size after serialization

 

Data creation→Time measurement→Serialization→Time measurement→Decialization→Time measurement


■MsgPack used

https://github.com/msgpack/msgpack-cli/releases

Expand the zip file in the above URL version 0.62 and
 You are using MsgPack.dll located in the unity3d folder.

■Json

with

MiniJSON

■Testcase
 1—
 (average serialized list of ints from 0 to 10000 100 times)
  Check it out on the actual Android device

2:
 
(average serialized list of strings from 0 to 10000 100 times)   Check it out on the actual Android device

■Test Results
 ■Case 1
  MsgPack (Array):
   Bytes after serialization: 29747
   serialization time: 24.6 msec
   Deserialization time: 165.2 msec

Json:
   Bytes after serialization: 48891
   serialization time: 31.1 msec
   Deserialization time: 74.7 msec

■ Case 2   MsgPack (Array):
   Bytes after serialization: 48893
   serialization time: 37.6 msec
   Deserialization time: 277.0 msec
  Json:
   Bytes after serialization: 68891
   serialization time: 22.2 msec
   Deserialization time: 57.5 msec

■source code

using UnityEngine;
using System;
using System.IO;
using System.Collections;
using System.Collections.General;
using MiniJSON;
using MsgPack;
using MsgPack.Serialization;
using System.Linq;

public class MsgPackAndJsonCompare:MonoBehavior {

public int SampleCount = 100;

private string MsgLog="";
private int SampleIndex = 0;


List<long>ArrayListLong=new List<long>();;
List<string>ArrayListString=new List<string>();



/// <summary>
/// int data creation process
/// </summary>
public void OnClickArrayInt()
{
    for (inti=0; i<10000;i++)
    {
        ArrayListLong.Add(i);
    }
    Debug.Log("OnClickArrayIntEnd");
}


/// <summary>
/// String data creation processing
/// </summary>
public void OnClickArrayString()
{
    for (inti=0; i<10000;i++)
    {
        ArrayListString.Add(i.ToString());
    }
    Debug.Log("OnClickArrayString End";

}


/// <summary>
/// Test start looping 100 times
/// </summary>
/// <param name="sendMode"></param>
public void OnClickCompare (int sendMode)
{
    SampleIndex=0;

    for (inti=SampleIndex;i<SampleCount;i++)
    {
        MsgLog=MsgLog+", LoopCnt="+i.ToString();
        MsgLog=MsgLog+", StartTime="+DateTime.Now.Ticks.ToString();

        MainFunc (sendMode);

        MsgLog=MsgLog+", EndTime="+DateTime.Now.Ticks.ToString()+"\n";
    }

    Debug.Log("Function End");


}

/// <summary>
/// main processing
/// </summary>
/// <param name="compareMode"></param>
private void MainFunc (int compareMode)
{
    // ArrayInt - MsgPack
    if(compareMode==0)
    {
        MsgLog=MsgLog+", serialize StartTime="+DateTime.Now.Ticks.ToString();
        varstream=new MemoryStream();
        // Create serializer instance.
        varserializer=MessagePackSerializer.Get<List<long>>();
        serializer.Pack (stream, ArrayListLong);

        // long datalen=stream.Length;

        MsgLog=MsgLog+", serialize EndTime="+DateTime.Now.Ticks.ToString();
        stream.Position=0;
        List<long>deserializedObject=serializer.Unpack(stream);
        MsgLog=MsgLog+", serialize EndTime="+DateTime.Now.Ticks.ToString();
    }
    // ArrayInt - Json
    else if (compareMode==1)
    {
        MsgLog=MsgLog+", serialize StartTime="+DateTime.Now.Ticks.ToString();
        string serialized = Json.Serialize (ArrayListLong);

        intilenb=System.Text.Encoding.GetEncoding(65001).GetByteCount(serialized);

        MsgLog=MsgLog+", serialize EndTime="+DateTime.Now.Ticks.ToString();
        List<System.Object>deserializedDoubleList=Json.Deserialize(serialized) as List<System.Object>;
        List<long>longList=deserializedDoubleList.OfType<long>().ToList();
        MsgLog=MsgLog+", serialize EndTime="+DateTime.Now.Ticks.ToString();

    }
    // ArrayString - MsgPack
    else if (compareMode==2)
    {
        MsgLog=MsgLog+", serialize StartTime="+DateTime.Now.Ticks.ToString();
        varstream=new MemoryStream();
        // Create serializer instance.
        varserializer=MessagePackSerializer.Get<List<string>>();
        serializer.Pack (stream, ArrayListString);

        long datalen=stream.Length;

        MsgLog=MsgLog+", serialize EndTime="+DateTime.Now.Ticks.ToString();
        stream.Position=0;
        List<string>deserializedObject=serializer.Unpack(stream);
        MsgLog=MsgLog+", serialize EndTime="+DateTime.Now.Ticks.ToString();
    }
    // ArrayString - Json
    else if (compareMode==3)
    {
        MsgLog=MsgLog+", serialize StartTime="+DateTime.Now.Ticks.ToString();
        string serialized = Json.Serialize (ArrayListString);

        intilenb=System.Text.Encoding.GetEncoding(65001).GetByteCount(serialized);

        MsgLog=MsgLog+", serialize EndTime="+DateTime.Now.Ticks.ToString();
        List<System.Object>deserializedDoubleList=Json.Deserialize(serialized) as List<System.Object>;
        List<string>longList=deserializedDoubleList.OfType<string>().ToList();
        MsgLog=MsgLog+", serialize EndTime="+DateTime.Now.Ticks.ToString();

    }
}


public void OnclickLog()
{
    StartCoroutine (LogDisp());

}


IEnumerator LogDisp()
{
    string[]LogArray=MsgLog.Split('\n');

    int maxloop = LogArray.Length;
    inti = 0;
    while(i<maxloop)
    {
        Debug.Log("Log="+LogArray[i]);

        yield return 0;

        i++;

    }
}

■Problems
 It says that MessagePack is faster when you collect information on the Internet.  I see a lot of them, but my source is
more than I do with MessagePack.  JSON is faster, so I suspect it's the wrong way.

c# unity3d json

2022-09-30 19:57

1 Answers

Serialization is the conversion of input data into binary, etc.
Decimalization is the opposite of binary and so on.

MessagePackSerializer serializes List<int> and decalizes it to List<int>.
However, while Json.Serialize serializes List<int>, Json.Deserialize has not returned to List<int>, only decoding the JSON string (is it returned?)

Therefore, it is not an equal comparison.

It doesn't seem to be understood at all, so I'll add it.
MessagePackSerializer supports any type if the conditions are met.For example,

public structure Color{
    public int R;
    public int G;
    public int B;
    public override string ToString() {return "Hoge";}
}

interprets the type, serializes the public properties, and returns a restore of each property value during de-serialization.
For MiniJSON does not maintain the type.Serialize ToString() results for unknown types, and de-serialize numbers to double or int64.
As a result, if you serialize Color above in MiniJSON, the JSON string will be "Hoge", and you can only get the string when you de-serialize it.

Therefore, it is not an equal comparison.


2022-09-30 19:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.