-
Notifications
You must be signed in to change notification settings - Fork 73
/
sort_1.cpp
51 lines (41 loc) · 1.36 KB
/
sort_1.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
#include <experimental/executor>
#include <experimental/future>
#include <algorithm>
#include <chrono>
#include <iostream>
#include <random>
#include <string>
using std::experimental::copost;
using std::experimental::use_future;
int main(int argc, char* argv[])
{
const std::string parallel("parallel");
const std::string serial("serial");
if (!(argc == 3 && (argv[1] != parallel || argv[1] != serial)))
{
std::cerr << "Usage: `sort parallel <size>' or `sort serial <size>'" << std::endl;
return 1;
}
std::vector<double> vec(std::atoll(argv[2]));
std::iota(vec.begin(), vec.end(), 0);
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(vec.begin(), vec.end(), g);
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
if (argv[1] == parallel)
{
copost(
[&]{ std::sort(vec.begin(), vec.begin() + (vec.size() / 2)); },
[&]{ std::sort(vec.begin() + (vec.size() / 2), vec.end()); },
use_future).get();
std::inplace_merge(vec.begin(), vec.begin() + (vec.size() / 2), vec.end());
}
else
{
std::sort(vec.begin(), vec.end());
}
std::chrono::steady_clock::duration elapsed = std::chrono::steady_clock::now() - start;
std::cout << "sort took ";
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
std::cout << " microseconds" << std::endl;
}