Fixed-Size Objects |
Variable-Size Objects |
Constant number of bytes (sizeof) |
Non-constant number of bytes |
C++ builtin type, class or struct Most binary file headers (e.g., BMP) Fixed-width ASCII records |
C++ string, vector, map, list, ... Delimited ASCII data JSON XML |
Very fast to allocate and deallocate |
Surprisingly slow to allocate and
deallocate (must use malloc or new) |
Easy to allocate in a C++ array |
Cannot be directly stored in an array (must
use pointers) |
Not extensible (tempted to squeeze bits) |
Can be extended (though you must plan ahead!) |
template <class array_ish> int do_the_thing(array_ish &arr) { arr[0]=12; arr[1]=34; return arr[0]+arr[1]; } int do_array(void) { int arr[2]; return do_the_thing(arr); } int do_vector(void) { std::vector<int> v(2); return do_the_thing(v); } void foo(void) { print_time("array",do_array); print_time("vector",do_vector); }
array: 1.44 ns/call vector: 16.50 ns/call