aboutsummaryrefslogtreecommitdiff
path: root/sort-benchmark/pairs/sort.cpp
blob: 77ab5f0f713989a690cf7accdd7ba30b91af3610 (plain)
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
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>

typedef struct { int a, b; } pair_t;
pair_t a[ARRAYSIZE];

int main(void) {
	srand(time(NULL));
	for (auto &x : a) {
		x.a = rand() % 1000000;
		x.b = rand() % 1000000;
	}

	struct timespec begin;
	clock_gettime(CLOCK_MONOTONIC, &begin);

	std::sort(a, a+ARRAYSIZE,
		[](const pair_t &x, const pair_t &y) {
			int d = x.a - y.a;
			return d < 0 || (d == 0 && x.b > y.b);
		});

	struct timespec end;
	clock_gettime(CLOCK_MONOTONIC, &end);
	double time = end.tv_sec - begin.tv_sec +
		(end.tv_nsec - begin.tv_nsec) / 1000000000.0;

	std::cout << "(" << a[ARRAYSIZE/2].a << ") C++ time for " << ARRAYSIZE
		<< " pairs: " << time << "s\n";
}

Generated with cgit - Back to sebastiano.tronto.net