fork download
  1. #include <vector>
  2. #include <cstdio>
  3. #include <ctime>
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <utility>
  7.  
  8. using namespace std;
  9.  
  10. int main() {
  11. int n = 100000,
  12. dummy = -1,
  13. n_remove_items = 10000;
  14.  
  15. vector<int> a(n, dummy);
  16. vector<int> b(n, dummy);
  17. vector<int> c;
  18.  
  19. clock_t start = clock(); {
  20. random_shuffle(begin(a), end(a));
  21. for (int i = 0; i < n_remove_items; i++) {
  22. a.erase(begin(a));
  23. }
  24. } clock_t end = clock();
  25. printf("a->size() = %d, %fsec.\n", a.size(), (double)(end - start) / CLOCKS_PER_SEC);
  26.  
  27. start = clock(); {
  28. for (int i = 0; i < n_remove_items; i++) {
  29. int index = rand() & b.size();
  30. b[index] = 0;
  31. }
  32. copy_if(begin(b), std::end(b), back_inserter(c), // XXX : duplication name(end)
  33. [&dummy](int x) {
  34. return x == dummy;
  35. });
  36. b = move(c);
  37. } end = clock();
  38. printf("b->size() = %d, %fsec.\n", b.size(), (double)(end - start) / CLOCKS_PER_SEC);
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0.84s 3228KB
stdin
Standard input is empty
stdout
a->size() = 90000, 0.843895sec.
b->size() = 99937, 0.001664sec.