forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path65-time_vector_search.cpp
66 lines (57 loc) · 1.56 KB
/
65-time_vector_search.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <stdexcept>
#include <vector>
#include "stopwatch.hpp"
bool found_in_vector(const std::vector<unsigned int> &V, unsigned int element)
{
for (auto x : V)
if (x == element)
return true;
return false;
}
int main()
{
Stopwatch BigS{};
BigS.start();
std::vector<unsigned int> V{};
{
Stopwatch S{};
std::cout << "Inserting 25000 elements" << std::endl;
S.start();
for (unsigned int i = 0; i < 25000; i++)
V.push_back(i * i);
S.stop();
std::cout << "Total time: " << S.elapsed() << " seconds" << std::endl;
}
{
Stopwatch S{};
std::cout << "Running 25000 valid lookups" << std::endl;
S.start();
for (unsigned int i = 0; i < 25000; i++)
{
if (!found_in_vector(V, i * i))
{
throw std::runtime_error{"Error with find()"};
}
}
S.stop();
std::cout << "Total time: " << S.elapsed() << " seconds" << std::endl;
}
{
Stopwatch S{};
std::cout << "Running 25000 invalid lookups" << std::endl;
S.start();
for (unsigned int i = 0; i < 25000; i++)
{
if (found_in_vector(V, i * i + 6))
{
throw std::runtime_error{"Error with find()"};
}
}
S.stop();
std::cout << "Total time: " << S.elapsed() << " seconds" << std::endl;
}
BigS.stop();
std::cout << "Absolute total time: " << BigS.elapsed() << std::endl;
return 0;
}