|
| ||
|| || >
|| > || > |
|| > | || > | >> > 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.
||
|| You are not supposed to get range checking unless you explicitly ask for
| it by
|| invoking the std::vector:at(n) member function.
||
|
| This is an implementation detail. The std library as supplied by MSFT
| (Dinkumware's implementation)) since 7.1 does range checking, don't know
| about other iplementations though. Point is that you should not rely on
| this, the standard does not enforce operator[] to be 'unchecked' AFAIK.
| The major difference between operator[] and at() access is that the former
| calls a "crt debugger" hook followed by a 'drwatson' call, while the
latter
| throws an "out_of_range" exception.
|
| Willy.
|
Forgot to mention that this behavior can be turned of by means of
the -D_SECURE_SCL switch or in code using:
#define _SECURE_SCL 0
Note that by doing so, you throw away all security related enhancements.
Willy.