-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShuffleSingle.cpp
More file actions
75 lines (59 loc) · 1.58 KB
/
Copy pathShuffleSingle.cpp
File metadata and controls
75 lines (59 loc) · 1.58 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
/*
Single Threaded Implementation
------------------------------
Shuffles random string tokens, each consisting of 8 capital letters
*/
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
#include <random>
#include <thread>
#include <mutex>
#include <chrono>
#include <unordered_set>
#include <fstream>
#include <stack>
using Clock = std::chrono::high_resolution_clock;
const unsigned int N = std::thread::hardware_concurrency(); //number of threads on device
std::vector<std::string> tokens;
const int token_count = 5e6;
const int token_length = 8;
int main()
{
tokens.reserve(token_count);
std::string token;
token.reserve(token_length);
auto generate_token = [&]()
{
token.clear();
for (int i=0; i < token_length; i++)
{
token += static_cast<char>(65+rand()%26);
}
return token;
};
for (int i=0; i<token_count; i++)
{
tokens.push_back(generate_token());
}
std::ofstream out("tokensbefore.txt");
for (auto& i: tokens)
{
out << i << '\n';
}
out.close();
auto start_time = Clock::now();
std::random_device rd;
std::mt19937 gen(rd());
std::shuffle(tokens.begin(),tokens.end(), gen);
auto end_time = Clock::now();
std::ofstream outafter("tokensafter.txt");
for (auto& i: tokens)
{
outafter << i << '\n';
}
outafter.close();
std::cout << "\nTime difference = "
<< std::chrono::duration<double, std::nano>(end_time - start_time).count() << " nanoseconds\n";
}