From 2f19e94b0ce78d6ccc9eedbf04b8a3460fd05565 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 29 Apr 2024 21:59:51 +0200 Subject: Sort benchmarks --- sort-benchmark/integers/Makefile | 20 ++++++++++++++++++++ sort-benchmark/integers/notes.txt | 11 +++++++++++ sort-benchmark/integers/sort.c | 29 +++++++++++++++++++++++++++++ sort-benchmark/integers/sort.cpp | 25 +++++++++++++++++++++++++ sort-benchmark/integers/sort_parallel.cpp | 28 ++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+) create mode 100644 sort-benchmark/integers/Makefile create mode 100644 sort-benchmark/integers/notes.txt create mode 100644 sort-benchmark/integers/sort.c create mode 100644 sort-benchmark/integers/sort.cpp create mode 100644 sort-benchmark/integers/sort_parallel.cpp (limited to 'sort-benchmark/integers') diff --git a/sort-benchmark/integers/Makefile b/sort-benchmark/integers/Makefile new file mode 100644 index 0000000..56a37ee --- /dev/null +++ b/sort-benchmark/integers/Makefile @@ -0,0 +1,20 @@ +CC = gcc +CPP = g++ +OPT = -O3 +VAR = -DARRAYSIZE=100000000 + +all: sort_c sort_cpp sort_parallel_cpp + +sort_c: + ${CC} ${OPT} ${VAR} -std=c11 -o sort sort.c + ./sort + +sort_cpp: + ${CPP} ${OPT} ${VAR} -std=c++20 -o sort sort.cpp + ./sort + +sort_parallel_cpp: + ${CPP} ${OPT} ${VAR} -std=c++20 -o sort sort_parallel.cpp -ltbb + ./sort + +.PHONY: sort_c sort_cpp sort_parallel_cpp diff --git a/sort-benchmark/integers/notes.txt b/sort-benchmark/integers/notes.txt new file mode 100644 index 0000000..7984803 --- /dev/null +++ b/sort-benchmark/integers/notes.txt @@ -0,0 +1,11 @@ +- Getting the parallel version to work was a pain. Two major gotchas: + (1) -ltbb had to be added *at the end* of the command line + (2) clang requires a libstdc++ option to make this work + of course everything was terrible to decipher with C++ compile errors + being the mess they are. +- For measuring time I could also use + clock_t begin = clock(); + ... + clock_t end = clock(); + double time = (dobule)(end - begin) / CLOCKS_PER_SEC; + but it does not work with the parallel version (it meausres CPU clocks). diff --git a/sort-benchmark/integers/sort.c b/sort-benchmark/integers/sort.c new file mode 100644 index 0000000..2a23d9d --- /dev/null +++ b/sort-benchmark/integers/sort.c @@ -0,0 +1,29 @@ +#define _POSIX_C_SOURCE 200809L /* Required to use clock_gettime */ + +#include +#include +#include + +int a[ARRAYSIZE]; + +int compar(const void *x, const void *y) { return *(int *)x - *(int *)y; } + +int main() { + srand(time(NULL)); + for (int i = 0; i < ARRAYSIZE; i++) a[i] = rand() % 1000000000; + + struct timespec begin; + clock_gettime(CLOCK_MONOTONIC, &begin); + + qsort(a, ARRAYSIZE, sizeof(int), compar); + + struct timespec end; + clock_gettime(CLOCK_MONOTONIC, &end); + double time = end.tv_sec - begin.tv_sec + + (end.tv_nsec - begin.tv_nsec) / 1000000000.0; + + printf("(%d) C time for %d numbers: %lfs\n", + a[ARRAYSIZE/2], ARRAYSIZE, time); + + return 0; +} diff --git a/sort-benchmark/integers/sort.cpp b/sort-benchmark/integers/sort.cpp new file mode 100644 index 0000000..3434d7f --- /dev/null +++ b/sort-benchmark/integers/sort.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +int a[ARRAYSIZE]; + +int main(void) { + srand(time(NULL)); + for (auto &x : a) x = rand() % 1000000000; + + struct timespec begin; + clock_gettime(CLOCK_MONOTONIC, &begin); + + std::sort(a, a+ARRAYSIZE, + [](const int &x, const int &y) { return x < y; }); + + 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] << ") C++ time for " << ARRAYSIZE + << " numbers: " << time << "s\n"; +} diff --git a/sort-benchmark/integers/sort_parallel.cpp b/sort-benchmark/integers/sort_parallel.cpp new file mode 100644 index 0000000..5de4158 --- /dev/null +++ b/sort-benchmark/integers/sort_parallel.cpp @@ -0,0 +1,28 @@ +/* Requires Thread Building Blocks (libtbb-dev on Debian) */ + +#include +#include +#include +#include +#include + +int a[ARRAYSIZE]; + +int main(void) { + srand(time(NULL)); + for (auto &x : a) x = rand() % 1000000000; + + struct timespec begin; + clock_gettime(CLOCK_MONOTONIC, &begin); + + std::sort(std::execution::par, a, a+ARRAYSIZE, + [](const int &x, const int &y) { return x < y; }); + + 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] << ") C++ time for " << ARRAYSIZE + << " numbers (parallel): " << time << "s\n"; +} -- cgit v1.3