Manipulate strings efficiently
The handling of string operations can affect the performance of your program.
- When you store strings into allocated storage, align the start of the
string on an 8-byte boundary.
- Keep track of the length of your strings. If you know the length of a
string, you can use mem functions instead of str functions.
For example, memcpy is faster than strcpy because it
does not have to search for the end of the string.
- If you are certain that the source and target do not overlap, use memcpy instead of memmove. This is because memcpy copies directly from the source to the destination, while memmove might copy the source to a temporary location in memory before
copying to the destination (depending on the length of the string).
- When manipulating strings using mem functions, faster code
will be generated if the count parameter is a constant
rather than a variable. This is especially true for small count values.
- Make string literals read-only, whenever possible. This improves certain
optimization techniques and reduces memory usage if there are multiple uses
of the same string. You can explicitly set strings to read-only by using #pragma strings (readonly) in your source files or -qro (this is enabled by default) to avoid changing your source files.