Looking at a post on CDSN, talking about std :: string efficiency than C-style char. Because std :: string uses a heap, and the C style string array can directly use the space on the stack in many cases, greatly improves the speed. In efficiency, a powerful weapon with C and C is the template Template. I tried to write a C style string class that is also built on the stack, and the efficiency should be good. Here is my post: High-efficient place to use std :: string is of course not good, this involves dynamic allocation of memory from HEAP, and Mutex protection may also be required in multithreaded Runtime DLL. If the size must be decided at runtime, C has to use Malloc, etc.
I mean what you mean should use the character array of characters constructed in the stack in the stack, such as Char BUF [222]. But this problem is that this BUF must allocate enough size, which is also why the buffer overflows often appear. We assume that this string is sufficient, and there is still a problem with the char array. It is that each Strcat is time to perform two similar Strlen operations, of course, the Intel CPU has a special instruction, such as scaSB.
But if you use a C style, you can do a CHAR array style String (without using std :: string) to avoid multi-time length of the string when Strcat calls are used. for example:
Inline size_t cpp_strcpy (char * dest, const char * src) {// copy src to dest and return the copied Len Return Strlen (STRCPY (DEST, SRC)); // Garbage Implementation, compilation}
Template
Local_String & Operator = (const char * str) {LEN = CPP_STRCPY (SZ LEN, STR); // The start address directly SZ LEN, avoid calling the string length again. Return * this;}
Local_string & operator = (const char * str) {len = cpp_strcpy (SZ, STR)); return * this;}
SIZE_T LEN () const {return len;} public: // public calculation. Char SZ [SIZE]; SIZE_T LEN;
In this way, on the same starting line (all CHAR ×) on the stack, the C style local_string is at least less than the C style STRCAT efficiency. Of course, we need a CPP_STRCPY, not Strcpy, which does not return a pointer, but returns how long it is, I have a garbage implementation. The function should be used to write a CPP_STRCPY function. EDI - Old_edi can be realised. Then use: local_string <40> str; str = "hello"; str = ","; str = "world"; efficiency should be higher than the C high because it saves the current string's valid length. Especially when you need to call =.
Then we need to look forward to the C compiler to inline all functions of local_string, and even do not need to instantiate specific local_string classes, avoid code expansion. -------------- Compilation is not familiar, CPP_STRCPY is too lazy, anyway, I can't use it now. After writing, you should look at the C style efficiency. After all, in Operator =, you can use SZ LEN directly to get the next position that needs to be copied immediately, and it has nothing to do with the length of the string, and there is also a wireless relationship. Test is not good test, and multiple cycles always feel very professional, it seems to be involved in cache hit rates. This C style has a problem that each local_string is inconsistent, which hinders the assignment, parameter transfer, etc. of the objects between different lengths of local_string instances. They should be allowed to share a base class to use Len as the first member.