C # and Unity memory optimization

Source: Internet
Author: User
Tags array to string

Reprinted from: http://www.xuanyusong.com/archives/1919

The client interacts with the server, using JSON and XML to feel that the amount of data is too large to affect efficiency. Using binary can save a lot of space.

The core class used when writing binary data is BinaryWriter, binary is binary meaning, visible operation binary write is used BinaryWriter. The commonly used data type allocates a fixed number of bytes, assuming that BinaryWriter writes a short then takes up 2 bytes, writes an int that takes up 4 bytes, and if it is an array, the array type byte length is multiplied by the array length.

byte: One byte (8 bits)
Short : Two bytes (16 bits)
int: Four bytes (32 bits) (one word length)
Long: Eight bytes (64 bits)
float: Four bytes (32 bits)
Double: eight bytes (64 bits)

and then talking about string, it's not a standard data type, it's an object, and its byte length is mutable. Also , BinaryWriter writes the length of the string as a byte, and then stores the length of the string in bytes. In parsing the string, you need to parse the string length and then convert the byte array to string by taking a byte array of the corresponding length after the length is obtained. When we write, we use BinaryWriter to read the BinaryReader.

C # and Unity memory optimization

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.