Why binary configuration files are more efficient than configuration files such as JSON

Source: Internet
Author: User

Why binary configuration files are more efficient than configuration files such as JSON

Project with spine animation, spine can export JSON and binary animation configuration file, the egg pain is spine officially did not provide the C binary configuration parsing code, but also did not provide its binary file format description. For the animation to load faster, you can only use the JSON to the binary file (format custom), and then self-implemented to parse the binary C code. After careful code implementation, the test result is that the binary configuration file is less than 1 and a half smaller than the JSON file size, and the animation is created more than 1 time times faster. The following is a brief summary of the advantages of the binary configuration file.

1. What is a binary configuration file

Of course, all files are stored in the binary (01) format in the computer, what is the binary configuration file? For example, to store

{  "time" = 133,  "COLOR" = [233, 0, 0],  "pos" = [+]     }

JSON is generally saved as text in utf-8 format, so what is Utf-8? It is a form of implementation of Unicode encoding, the specific explanation please refer to UTF-8 coding Rules (GO), CJK Uincode Coding Reference. In other words, like the number type in the program 133,233,22, a uint8 can be stored down, but 133 in JSON accounted for 3 bytes, if you save a 12.432312 and other data to occupy more space. In this paper, the binary configuration, directly stored 133, such as the uint8 binary encoding 0x85, which reduces the size of a part of the file. Encoding and decoding can be negotiated, such as time, codec are replaced by T, but also can save some space, and even can not store time,color,pos and other keys, direct order in the configuration of the value, decoding directly read value (in order to say clearly, The following example retains the key), which can reduce the size of the configuration file.

2. Advantages and disadvantages of binary configuration files

Advantages have been said, 2 advantages: Small files, fast parsing. Why is the file small above already said. As for why parse fast, first, because the file is small, Io time needs less. Second, the less to parse the configuration file into a JSON object or XML object intermediate steps, directly read the binary file can be, I think along the reading of binary files can achieve the fastest speed.

There are also 2 drawbacks: difficult to view directly, poor versatility. Configuration files such as JSON or XML can be directly seen to configure the God horse, but this binary is nothing to see. The general difference is because to achieve efficient resolution, it is best to follow the analysis, the configuration file is finished, so parsing the binary code is difficult to common.

3. Read and write binary examples

For example, the above JSON file, how to make binary configuration, you can first specify the type of data, such as s for String,i to number,f for float. String also tells the parsing how long the string is, such as time is s4time. The above example to write binary is s4timei133s5colori244i0i0s3posi34i22, which is certainly not optimal, is to explain how to read and write binary.

(1) Python writes binary files

Python writes binary files used to Struct.pack, read binary with Struct.unpack (), specifically how to use the struct please refer to using Python for binary file read and write

Format C Type Python Number of bytes
X Pad byte No value 1
C Char string of length 1 1
B Signed Char Integer 1
B unsigned char Integer 1
? _bool bool 1
H Short Integer 2
H unsigned short Integer 2
I Int Integer 4
I unsigned int Integer or Long 4
L Long Integer 4
L unsigned long Long 4
Q Long Long Long 8
Q unsigned long long Long 8
F Float Float 4
D Double Float 8
S Char[] String 1
P Char[] String 1
P void * Long

Import Structdef writebinary ():    binaryfile = open (r "./binary.obj", "WB");    Binaryfile.write (Struct.pack ("I", 4));    Binaryfile.write (Struct.pack ("4s", "time"));    Binaryfile.write (Struct.pack ("I", 133));    Binaryfile.write (Struct.pack ("I", 5));    Binaryfile.write (Struct.pack ("5s", "color")), if __name__ = = "__main__":    writebinary ();

After execution, the following binaries are generated

(2) C + + read binary file

#include <iostream>using namespace Std;char * data = Nullptr;char * getfilecontent (const char * filePath) {FILE    *pfile=fopen (FilePath, "R");    char * PBUF;    Fseek (pFile, 0, seek_end);    int len = Ftell (pFile);    PBuf = new Char[len+1];    Rewind (PFile);    Fread (PBuf, 1, Len, pFile);    Pbuf[len] = 0;    Fclose (PFile); return pBuf;} int8_t ReadByte () {return (int8_t) *data++;}    int32_t readInt () {int32_t val = 0;    char * PD = (char*) &val;    *PD = ReadByte ();    * (PD + 1) = ReadByte ();    * (PD + 2) = ReadByte ();    * (PD + 3) = ReadByte (); Return (int32_t) Val;    std::string readString () {int32_t strLen = readInt ();    std::string str = std::string (data, strLen);    data = data + StrLen; return str;}    int main () {data = Getfilecontent ("/users/guanfeng/pycharmprojects/binarytest/binary.obj");    std::string timestr = readString ();    int timevalue = ReadInt ();    std::string colorstr = readString (); Std::cout << "Timestr is" << timestr << Std::endl;    Std::cout << "Colorstr is" << colorstr << Std::endl;    Std::cout << "TimeValue is" << timevalue << Std::endl;    Free (data); return 0;}

Result is

Why binary configuration files are more efficient than configuration files such as JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.