Is
int array[100] = {};
faster than
int array[100];
for(int i=0; i<100; ++i){
array[i] = 0;
}
Or are they equal? What are the differences if any?
The initialisation of non-statically allocated arrays might well be implemented the same way for both shown variants. You will have to measure or look at the generated assembly.
For statically allocated data (namespace scope data in C++ parlance), on UNIX there is the BSS segment for zero-initialized data and the data segment for non-zero-inialized data. Symbols places in the BSS segment are only specified in location and size, their content is implicitly zero and occupies no size in the executable. I'd certainly try to take advantage of zero-initialization for big arrays. (However, most of the time I'm dealing with big arrays, I don't know how big they will have to be and I have to allocate and intialialise them dynamically anyway.)
Once you need initial values different from zero, their compile-time initialization will occupy space in the executable (data segment) and you're facing a classic space/time tradeoff.
Given that today CPU speed is much faster than memory and disk bandwith, dynamic initialization will carry you a long way and also is more flexible.
It largely depends on your compiler, my guess is with appropriate optimizations turned on, both will be the same. It also depends on what you do with the values afterwards. If array
goes out of scope immediately, it won't be created at all. If the values are just read, 0
might be directly available and an actual read might not even happen at all. Your best bet is to profile for your specific use-case.
Note that this only works for initialization to 0
, if you need another value the first wouldn't be an option.
This code:
int array[100] = {};
Means the compiler is free to do whatever it wants to initialize the array. This might mean baking the initialization into the compiled code in such a way that the initialization takes zero, or at least constant time. Potentially this could be O(1) performance, but there's no guarantee.
This code on the other hand:
int array[100];
for(int i=0; i<100; ++i){
array[i] = 0;
}
is O(N). It will never be less than O(N) because you have a for loop in it. Perhaps in some cases a compiler might be able to see it can optimize away the for loop, but it's going to be a harder problem for it.
So static initialization can be faster, but isn't necessarily. A for loop will almost certainly not be faster.
int array[100] = {};
This will default-initialize an array, which means that for basic (scalar) types the entire array will be properly zero-initialized and indeed in O(1) time. since its compiler implementations it is bound to be optimized.
int array[100];
for(int i=0; i<100; ++i){
array[i] = 0;
}
here the programmer steps up and takes the responsibility to initialize the array. Now it depends on how well compiler optimization code is written to answer whether it's at par with the previous initialization or may fall short .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With