|
Page 1 of 12 Introduction
Given the rich provision of normal and template classes in MFC, it is common for programmers in Visual C++ to ignore the standard template library.
vector and CPtrArray
These are the two most easily compared array classes and we ran a simple benchmark test adding 100,000 and 200,000 objects to the end of an array, then individually removing the objects from the start of the array.
We also included in the test, as a point of thoroughness, the CEzArray class used in some marsjupiter.com projects as a lightweight array class for arrays with less than an unsigned short number of elements, obviously we modified this to allow for the large number of elements in the test. Since we never layed any great claims for CEzArray in terms of efficiency we were expecting it to come last.
| Array Type |
100,000 members |
200,000 members |
| vector |
7.98 secs |
65.06 secs |
| CPtrArray |
8.03 secs |
65.24 secs |
| CEzArray |
8.25 secs |
64.98 secs |
The results were interestingly close for all array types, with a clear non linear degradation in performance with the increase in members. On analysis the reason they are all close is that the vast bulk of the time is taken up with deletion. This comes down to constant memory shifting and no class can have any special cleverness for this.
Repeating the test with larger numbers allocated, but removing all deletions from the test:
| Array Type |
2000,000 members |
4000,000 members |
| vector |
0.75 secs |
1.55 secs |
| CPtrArray |
22.23 secs |
84.95 secs |
| CEzArray |
22.2 secs |
88.03 secs |
Here is is apparent that the time differences are huge and are to do with the reallocation strategies of the different array types. Each time an element is added to an array, the array makes space not only for the element added, but it should also make a guess at how many more elements are likely to be added. This avoids constant memory reallocation and it seems from these tests that vector wins hands down.
Of course more testing might reveal the exception that proves the rule and the more liberal allocation strategy of vector might is some circumstances allocate an excess of memory.
<< Start < Prev 1 2 3 4 5 6 7 8 9 10 Next > End >> |