Suggestion 1: operate strings correctly. We recommend that you operate strings.

Source: Internet
Author: User

Suggestion 1: operate strings correctly. We recommend that you operate strings.

How to avoid the extra performance overhead caused by carelessness:

1. Make sure to pack as few items as possible

In your own code, you should avoid writing unnecessary Packing code as much as possible.

The reason for the performance loss caused by the packing operation:

A. First, memory is allocated for the value type in the managed heap. In addition to the memory allocated by the value type itself, the total memory must be added with the memory occupied by the Type object pointer and the synchronized block index.

B. assign a value of the value type to the newly allocated heap memory.

C. Return the address of the object that has become the reference type.

 

2. Avoid allocating additional memory space

For CLR, A string object is a special object. Once assigned, it cannot be changed. Call System. any method in the String class or any operation (such as "=" or "+") will create a new String object in the memory, allocate new memory space for the new object.

Use StringBuilder for string operations, or use the string. Format Method for string operations (the string. Format method uses StringBuilder for string formatting internally ).


Correct string Assignment Statement

Why char s [5] = {'A', 'B', 'C', 'D', 'E'}; no

In char [n], c ++ requires a save Terminator '\ 0' by default, so char [n] can only store n-1 characters at most, otherwise, an error will occur (the last character cannot be correctly output). It should be defined as follows:
Char s [6] = {'A', 'B', 'C', 'D', 'E '}; // you can add '\ 0' to the end:
Char s [6] = {'A', 'B', 'C', 'D', 'E', '\ 0 '};
Char * s; first defines a string pointer without initialization; when s = {"ABCDEF, the string "ABCDEF" is used for initialization (or s points to the string "ABCDEF"). At this time, the system automatically adds '\ 0' to the end of s, so it is correct, can be output.

If you are learning c ++, we recommend that you use string to use less char.
String is convenient, fast, secure, and reliable.
Of course, char operations cannot be ignored as part of learning. The internal implementation of string is based on char.

How to perform Python string operations?

It reasonably combines high performance and makes programming simple and interesting. The following describes the python string operation process. To use the string method, you must first import the string method. However, due to the suggestions of many python users, the string method is changed from python2.0 to S. method (), as long as S is a string object, it can be used in this way, without the need for import. At the same time, in order to maintain backward compatibility, the current Python still retains a string module. Describes how to compile a Python program. What is the Python interpreter and how to use it? In-depth analysis of how Python Web applications can be correctly installed? The methods defined in the global variables of Python are the same as those of Python string operations. All these methods finally point to the functions called using S. method. Note that S. method () can call more methods than the string module. For example, isdigit () and istitle () can only be called in the S. method () method. For a String object, the first thought of operation may be to calculate the number of characters it contains, and it is easy to think of S. len (), but this is wrong. It should be len (S ). Because len () is a built-in function, including in the _ builtin _ module. Python string operations do not include len () in the string type. At first glance, it seems a bit incomprehensible. In fact, everything has its own logic. Len () can calculate not only the number of characters in a string. It can also calculate the number of list members and the number of tuple members. Therefore, it is inappropriate to calculate len () in a single string. Therefore, len () can be used as a general function. Using overload to perform different types of operations, you can also include a len () function in each type of len () operation. Python chooses the first solution. Similarly, the str (arg) function represents arg in the string type. Character case-insensitive conversion in a string: S. lower () # lower case S. upper () # uppercase S. swapcase () # case-insensitive S. capitalize () # capital String. the alignment of the capwords (S) string in the output: S. lower () # lower case S. upper () # uppercase S. swapcase () # case-insensitive S. capitalize () # capital String. search and replace in capwords (S) strings: S. lower () # lower case S. upper () # uppercase S. swapcase () # case-insensitive S. capitalize () # capital String. capwords (S) use the post-production translation table of the above function to translate S and delete some characters in deletechars. Note that if S is a unicode string, the deletechars parameter is not supported. You can use the same function to translate a character into a python string operation. In addition, you can use the functions of the codecs module to create more powerful translation tables.

Related Article

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.