char buffer[1000] = {0};
This initializes all 1000 elements to 0. Is this constant time? If not, why?
It seems like the compiler could optimize this to O(1) based on the following facts:
- The array is of fixed size and known at compile time
- The array is located on the stack, which means that presumably the executable could contain this data in the data segment of the executable (on Windows) as a chunk of data that is already filled with 0’s.
Note that answers can be general to any compiler, but I’m specifically interested in answers tested on the MSVC compiler (any version) on Windows.
Bonus Points: Links to any articles, white papers, etc. on the details of this would be greatly appreciated.
It will never be constant time, global or not. its true the compiler initializes that, but the operating system must load all the file into memory, which takes
O(n)time.