|
| | >> > The functionality offered by a list will cost. Maybe only in adding
| >> > and not in getting. But it will cost.
| >>
| >> std::vector provides access time equal to the access time of unmanaged
array
| >> access, yet can dynamically grow.
| >> Does .NET have anything with EXACTLY these performance characteristics?
| >
| > Frankly, I doubt that std::vector provides that, to be honest. Assuming
| > it's an array + size, you need to do a size check before the array
| > access, so there's a performance penalty there, even if it's really
| > small.
|
| No there is no size check, and this cost is not be really small, it at
least
| doubles the access time.
|
This is definitely not true, consider following small sample:
#include <vector>
int main(int argc, char *argv[])
{
size_t size = 100;
std::vector<int> array(size); // make room for 10 integers,
// and initialize them to 0
// do something with them:
for(int i=0; i<size; ++i){
array = i;
}
// break here, disassembly follows
int v = array[55];
return 0;
}
comments added [n]
004025e8 85ff test edi,edi [1]
004025ea 740a je vect!main+0x56 (004025f6)
004025ec 2bdf sub ebx,edi [2]
004025ee c1fb02 sar ebx,2 [3]
004025f1 83fb37 cmp ebx,37h [4]
004025f4 7705 ja vect!main+0x5b (004025fb)
004025f6 e8f1070000 call vect!_invalid_parameter_noinfo (00402dec)
004025fb 8b8fdc000000 mov ecx,dword ptr [edi+0DCh]
ds:0023:00324c3c=00000037 [5]
[1] checks array base pointer for null (non initialized)
[2] calculate length of array in bytes (edi = base, edx points to past end
of array)
[3] convert to length in int's (divide by 4)
[4] index < length of array
[5] move int at edi+55 -> ecx
You see, there is a pointer validation check and a bounds check for each
access. This accounts for 6 instructions, supposed index is within bounds.