/
exercise3.cpp
67 lines (51 loc) · 1.82 KB
/
exercise3.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
67
// -----------------------------------------------------------------------------
// https://lptcp.blogspot.com/
//
/*
Chapter 5 - Exercise 3
(*2) Time a loop. Write out the time in milliseconds. Do this for the default
setting of your compiler and for a setting using an optimiser (e.g., -O2 or
"release"). Be careful not to have the optimiser eliminate your whole loop
as dead code because you did not use a result.
*/
// https://github.com/l-paz91/
// -----------------------------------------------------------------------------
//--INCLUDES--//
#include <map>
#include <thread>
#include <chrono>
#include "std_lib_facilities.h"
// -----------------------------------------------------------------------------
int timeLoop()
{
// this was taken from
// https://github.com/l-paz91/principles-practice/blob/master/Chapter%2026/Exercise%2014
auto start = chrono::high_resolution_clock::now();
int firstSortSize = 500000;
cout << "Creating first map...\n";
auto timeNow = chrono::system_clock::now();
map<string, int> firstSet;
int sum = 0;
for (int i = 0; i < firstSortSize; ++i)
{
firstSet.insert({ randString(randint(100)), i });
sum += i;
}
auto end = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(end - start).count();
cout << "Time taken: " << duration << " milliseconds\n";
return sum;
}
// -----------------------------------------------------------------------------
int main()
{
int sum = timeLoop();
cout << "Sum: " << sum << endl;
// 740ms for release
// 8272ms for debug
keep_window_open();
return 0;
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------