-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSort.cpp
More file actions
47 lines (40 loc) · 1.48 KB
/
Sort.cpp
File metadata and controls
47 lines (40 loc) · 1.48 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
#include <vector>
#include <algorithm>
#include <execution>
#include <iostream>
#include <iterator>
#include <chrono>
#include <random>
#include <limits>
int main()
{
std::random_device rnd_device;
std::mt19937 mersenne_engine{ rnd_device() };
unsigned long l = 0;
auto gen = [&l]() {
return l++;
};
std::vector<unsigned long> v(10000000);
std::generate(std::begin(v), std::end(v), gen);
std::shuffle(std::begin(v), std::end(v), mersenne_engine);
// standard sequential sort
std::vector v1 = v;
auto start = std::chrono::high_resolution_clock::now();
std::sort(v1.begin(), v1.end());
std::cout << (std::chrono::high_resolution_clock::now() - start).count() / 1000000000.0 << std::endl;
// sequential execution
std::vector v2 = v;
start = std::chrono::high_resolution_clock::now();
std::sort(std::execution::seq, v2.begin(), v2.end());
std::cout << (std::chrono::high_resolution_clock::now() - start).count() / 1000000000.0 << std::endl;
// permitting parallel execution
std::vector v3 = v;
start = std::chrono::high_resolution_clock::now();
std::sort(std::execution::par, v3.begin(), v3.end());
std::cout << (std::chrono::high_resolution_clock::now() - start).count() / 1000000000.0 << std::endl;
// permitting parallel and vectorized execution
std::vector v4 = v;
start = std::chrono::high_resolution_clock::now();
std::sort(std::execution::par_unseq, v4.begin(), v4.end());
std::cout << (std::chrono::high_resolution_clock::now() - start).count() / 1000000000.0 << std::endl;
}