You asked about complexity, but you probably intended to ask about performance.
The complexity, denoted by O (n), is a concept regarding how the number of operations in an algorithm is forced to grow as the size of the problem increases. O (n) means that a certain number of steps must be performed proportional to the size of the input. He does not say what proportion it is. memset - O (n). O (n 2 ) means that a certain number of steps proportional to n 2 must be performed. memset is not O (n 2 ), since setting 2n bytes takes twice as much work as n bytes, and not four times as much work.
You are most likely interested in memset performance because the library version of memset is much faster than the C version you could write.
The library version is much faster because it uses specialized instructions. The most common modern processors have instructions that allow them to write 16 bytes to memory in a single instruction. Library developers write critical functions, such as memset in assembly language or something close to it, so they have access to all of these instructions.
When you write in C, it is difficult for the compiler to use these instructions. For example, a pointer to the memory you are installing may not coincide with a multiple of 16 bytes. The authors of memset will write code that checks the pointer and branches for different code for each case in order to set individual bytes separately and then align the pointer so that they can use quick instructions that store 16 bytes for a while. This is just one of several complications that library developers face when writing routines such as memset.
Due to these difficulties, the compiler cannot easily take your C memset implementation and turn it into fast code that experts write. When the compiler sees a loop in C code that writes one byte at a time, it usually generates an assembly language that writes one byte at a time. Optimizers are getting smarter, but complications limit how much they are allowed to do and how much they can do without generating a lot of code to handle cases that can rarely occur.
source share