What is paging effect in C++?
I came across this as I was trying to learn array and vectors in c++. What is the "paging effect" mentioned in the post? Also, just to check my own understanding, I think vector uses more time is because of the dynamic memory allocation. Am I right?
additional question:
but with vector<int> arr( 10000 )
isn't there already enough memory for 10000 int allocated? or put it this way, does arr
still grow if all I do is iterating through each element开发者_如何学运维 and initializing them?
Vector uses dynamic allocation if you use push_back(), but you can force it to pre-allocate memory with reserve().
Checked builds (common in debug libraries) also check the bounds for vector operations which can slow them down in debug mode. Release builds should be no slower than raw C.
Paging means moving memory out to disk when physical memory is full. You have to be careful with timing if you think memory is being paged out. A common technique is to run the task multiple times and reject the longest times.
edit: You should (almost) never use the raw 'C' type instead of the STL for efficiency. The people that wrote the STL are both really smart and really care about performance. Used properly it should never be worse than 'C' and is often better. The same goes double for using STL algorithms rather than your own han rolled loops.
The page you linked to points out that optimisation removes the performance difference. This means it is most likely caused by extra function calls in vector - you can safely ignore these because the optimiser is smart enough to inline them.
The poster is using the name "paging effect" but what they are actually referring to in the vector case is the cost of memory allocation. Also, by trying to write / read to that memory, they are pulling a segment at the end of the arrays into the cache, perhaps improving future performance in that memory area.
The author of that code is forcing
array[size - 1] = 0;
access to make sure that at least one access to the array buffer has been made prior to running the main part of his code. This is done to mitigate effects of using virtual memory swap file - that access increases the probability of the buffer not being swapped out to the paging file when the main code starts. Without this it could happen that when the first access to the buffer was made swapping would occur and that would increase the runtime significantly and lead to false meausurement results.
This will only have guaranteed effect if the buffer size is not greater than the size of the minimum memory segment the operating system uses for addressing virtual memory.
This is not a problem specific to C++.
Also vector is slower because accessing it requires two memory accesses - one to retrieve the buffer address, another one to actually access memory. With C array the buffer address is already known and only one access is needed. If the same code with vector is rewritten using a temporary for storing the buffer address it runs much faster.
精彩评论