i'm in progress of Dapp developping.
My question is, in regards of gas economics,
bool[] a = new bool[](16);
and
uint16 a = 0;
Both has same gas cost?
Size of array will be constant.
bool[] a = new bool[](16);
Initialization of a storage fixed-size array creates pointers to N+1 storage slots, where N is the array length. Value stored in the first slot is the array length (in your case 16), the rest are values of its items.
In this case (not using struct
s), there is no optimization related to the datatype length. So even bool
takes up the whole (32byte) slot.
uint16 a = 0;
Initialization of a storage integer creates a pointer to only 1 storage slot.
You can also save one write operation not using the redundant assign of 0
value, because 0
is the default value anyway. So you can do uint16 a;
instead of uint16 a = 0;
So in short, it's ~17 times cheaper to initialize the uint
(without assigning the zero value) compared to initialization of the array.