forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path63-time_vector_insert.cpp
43 lines (37 loc) · 1.31 KB
/
63-time_vector_insert.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <stdexcept>
#include <vector>
#include "stopwatch.hpp"
int main()
{
const int num_elements = 200000;
Stopwatch S{};
{ //Create a new scope so that V is destroyed at the end of this test (and recreated for the next one)
std::vector<unsigned int> V{};
std::cout << "Inserting " << num_elements << " elements (push_back)" << std::endl;
S.start();
for (unsigned int i = 0; i < num_elements; i++)
V.push_back(i * i);
S.stop();
std::cout << "Total time: " << S.elapsed() << " seconds" << std::endl;
}
{
std::vector<unsigned int> V{};
std::cout << "Inserting " << num_elements << " elements (insert() at end)" << std::endl;
S.start();
for (unsigned int i = 0; i < num_elements; i++)
V.insert(V.end(), i * i);
S.stop();
std::cout << "Total time: " << S.elapsed() << " seconds" << std::endl;
}
{
std::vector<unsigned int> V{};
std::cout << "Inserting " << num_elements << " elements (insert() at beginning)" << std::endl;
S.start();
for (unsigned int i = 0; i < num_elements; i++)
V.insert(V.begin(), i * i);
S.stop();
std::cout << "Total time: " << S.elapsed() << " seconds" << std::endl;
}
return 0;
}