Wise memory, storing a string in a byte is cheaper than its UTF equivalent?

If I save the string as a byte, does it use less memory than if it were stored in UTF-8?

eg.

string text = "Hello, World!";

Compared to encoding into a byte variable?

+3
source share
4 answers

If you saved this in a byte array, it will be more efficient than a string, yes - because all this text is ASCII, which will be encoded as one byte per character. However, this is not universally true for all strings (some characters will occupy 2 bytes, and some will occupy 3, and for characters without BMP even more), as well as damned vision, less convenient for working in binary form ...

, .

+3

UTF8 1 char, 7 ascii.

.NET UCS-2, 2 char IIRC, , , UTF8, , , , (aka, latin1).

+3

, , UTF-8 , ASCII, - UTF8 , UTF-16.

//UTF-16 so 26 bytes
string text = "Hello, World!";

//UTF-8 length will be 13 (only ASCII chars used)
var bytesUTF8 = Encoding.UTF8.GetBytes(text);

//UTF-16 so 26 bytes
var bytesUTF16 = Encoding.Unicode.GetBytes(text);
+1

- , .NET UTF-16. char Int16 ( ) ( char ).

If you are dealing only with ASCII, yes, you can put the string in an array of bytes, which takes up half the space as a char array and does not lose information. However, as John said, this is not a very convenient way to work with strings. You have 2 GIGABYTES addresses available for one line. As bytes, yes, you get 2 billion characters, but as strings, you still get 1 BILLION characters in one line. If you really need more than one line, I'm worried about what you think you need.

0
source

Source: https://habr.com/ru/post/1796352/


All Articles