Ticket #823 (closed: fixed)
General cleanup of std::vector usage
Reported by: | Martyn Gigg | Owned by: | Nick Draper |
---|---|---|---|
Priority: | major | Milestone: | Iteration 19 |
Component: | Keywords: | ||
Cc: | laurent.chapon@… | Blocked By: | |
Blocking: | Tester: |
Description
From Laurent,
I can see that at a lot of different places in the code (and I have probably done it myself as well), we simply initialize a vector (with zero size) and then call push_back a number of times on it. Well, since our loops are pretty long usually, we are actually reallocating a quite substantial number of times. It would be very sound in my opinion to put a reserve(N) command before entering the loop because most of the time we can guess the max size (say nHistograms). Below is a test of MedianDetectorTest running on WISH. The vector nums get reallocated about 20 times (with memory swap every time) and its size is doubled every time.
It might be different under windows...
Number of reallocation in MedianDetectorTests for WISH This is the implication of not using reserve after creating a vector 0
1 Adress0x7f4489a78e70 2 Adress0x7f4489a78e50 4 Adress0x7f449bb7cfc0 8 Adress0x7f4486e7bc80 16 Adress0x7f4486e7bcd0 32 Adress0x7f4486e7b8a0 64 Adress0x7f43d90fd830 128 Adress0x7f448e377e70 256 Adress0x7f448b677e70 512 Adress0x7f448dd77e70 1024 Adress0x7f4490d7af40 2048 Adress0x7f44a4a789f0 4096 Adress0x478d3a0 8192 Adress0x47953b0 16384 Adress0x7f43a92dc760 32768 Adress0x7f43a9100010 65536 Adress0x7f43a9140020 131072 Adress0x436aee0