Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 813
derrdji Avatar asked Nov 27 '25 20:11

derrdji


2 Answers

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.

like image 114
Martin Beckett Avatar answered Nov 30 '25 11:11

Martin Beckett


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.

like image 30
Tom Leys Avatar answered Nov 30 '25 09:11

Tom Leys



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!