aboutsummaryrefslogtreecommitdiff
path: root/sort-benchmark/pairs/sort.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--sort-benchmark/pairs/sort.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/sort-benchmark/pairs/sort.cpp b/sort-benchmark/pairs/sort.cpp
new file mode 100644
index 0000000..77ab5f0
--- /dev/null
+++ b/sort-benchmark/pairs/sort.cpp
@@ -0,0 +1,32 @@
1#include <algorithm>
2#include <cstdlib>
3#include <ctime>
4#include <iostream>
5
6typedef struct { int a, b; } pair_t;
7pair_t a[ARRAYSIZE];
8
9int main(void) {
10 srand(time(NULL));
11 for (auto &x : a) {
12 x.a = rand() % 1000000;
13 x.b = rand() % 1000000;
14 }
15
16 struct timespec begin;
17 clock_gettime(CLOCK_MONOTONIC, &begin);
18
19 std::sort(a, a+ARRAYSIZE,
20 [](const pair_t &x, const pair_t &y) {
21 int d = x.a - y.a;
22 return d < 0 || (d == 0 && x.b > y.b);
23 });
24
25 struct timespec end;
26 clock_gettime(CLOCK_MONOTONIC, &end);
27 double time = end.tv_sec - begin.tv_sec +
28 (end.tv_nsec - begin.tv_nsec) / 1000000000.0;
29
30 std::cout << "(" << a[ARRAYSIZE/2].a << ") C++ time for " << ARRAYSIZE
31 << " pairs: " << time << "s\n";
32}

Generated with cgit - Back to sebastiano.tronto.net