-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
115 lines (92 loc) · 3.07 KB
/
main.cpp
File metadata and controls
115 lines (92 loc) · 3.07 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <vector>
#include <list>
#include <set>
using std::list;
using std::vector;
using std::string;
using std::set;
namespace helpers
{
template <class Container, class Value>
struct stlext
{
static typename Container::const_iterator
lowerBound( const Container &c, Value value) { return std::lower_bound(c.cbegin(), c.cend(), value); }
};
template <class Value>
struct stlext<set<Value>, Value>
{
static typename set<Value>::const_iterator
lowerBound (const set<Value> &c, Value value) { return c.lower_bound(value); }
};
unsigned executeProcedureMeasuredMicroseconds(std::function<void()> func)
{
auto startTime = std::chrono::high_resolution_clock::now();
func();
auto finishTime = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(finishTime - startTime).count();
}
template<class Container>
void printContainer(const Container &c, const string &title)
{
std::cout << title << "\t" << std::endl;
for (const auto & v : c)
std::cout << v << ",";
std::cout << std::endl;
}
}
template<class Container>
void do_test_insert_random_position(Container &container, unsigned actions)
{
while (actions--)
{
const auto nextElement = rand() % 10000;
const auto insertPosition = helpers::stlext<Container, typename Container::value_type>::lowerBound(container, nextElement);
container.insert(insertPosition, nextElement);
}
}
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "N argument missing. Aborting\n";
return 1;
}
bool verbose = false;
if (argc > 3)
{
if (string(argv[3]) == "-v")
verbose = true;
}
auto N = std::atoi(argv[1]);
const auto M = std::atoi(argv[2]);
for (auto round = 0; round < M; ++round)
{
vector<int> v;
list<int> l;
set<int> s;
v.reserve(N);
const auto vectorElapsedMicro = helpers::executeProcedureMeasuredMicroseconds([&]() {
do_test_insert_random_position(v, N);
});
std::cout << round + 1 << "," << N << ": vector elapsed microseconds: " << vectorElapsedMicro << std::endl;
const auto listElapsedMicro = helpers::executeProcedureMeasuredMicroseconds([&]() {
do_test_insert_random_position(l, N);
});
std::cout << round + 1 << "," << N << ": list elapsed microseconds: " << listElapsedMicro << std::endl;
const auto setElapsedMicro = helpers::executeProcedureMeasuredMicroseconds([&]() {
do_test_insert_random_position(s, N);
});
std::cout << round + 1 << "," << N << ": set elapsed microseconds: " << setElapsedMicro << std::endl;
if (verbose)
{
helpers::printContainer(v, "vector");
helpers::printContainer(l, "list");
helpers::printContainer(s, "set");
}
N *= 2;
std::cout << std::endl;
}
return 0;
}