From aae5f00dbd8c2e20c07967087645f63e17ec7eb4 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 4 Jul 2020 00:47:06 +0200 Subject: [PATCH] Add new benchmark --- bench/Makefile | 6 +++ bench/bench-find-sort.cpp | 101 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 bench/bench-find-sort.cpp diff --git a/bench/Makefile b/bench/Makefile index f3640a76..5e49dbea 100644 --- a/bench/Makefile +++ b/bench/Makefile @@ -410,6 +410,12 @@ bench-string-pgo: ############################################################################ +bench-find-sort: + g++ bench-find-sort.cpp -O2 -march=native -std=c++11 -I.. -g common.c -o bench-find-sort.exe + ./bench-find-sort.exe 10000000 + +############################################################################ + clean: $(RM) *.o bench-*.exe $(RM) bench-*.s *~ massif.out.* diff --git a/bench/bench-find-sort.cpp b/bench/bench-find-sort.cpp new file mode 100644 index 00000000..da17ec7a --- /dev/null +++ b/bench/bench-find-sort.cpp @@ -0,0 +1,101 @@ +// program to find longest contiguous subsequence +// based on https://www.geeksforgeeks.org/longest-consecutive-subsequence/ +#include +#include + +using namespace std; + +// Returns length of the longest contiguous subsequence +int cpp_find_longest(int arr[], int n) +{ + unordered_set S; + int ans = 0; + + // Hash all the array elements + for (int i = 0; i < n; i++) + S.insert(arr[i]); + + // check each possible sequence from + // the start then update optimal length + for (int i = 0; i < n; i++) { + // if current element is the starting + // element of a sequence + if (S.find(arr[i] - 1) == S.end()) { + // Then check for next elements + // in the sequence + int j = arr[i] + 1; + while (S.find(j) != S.end()) + j++; + + // update optimal length if + // this length is more + ans = max(ans, j - arr[i]); + } + } + return ans; +} + +#include "m-dict.h" + +static inline bool oor_equal_p(int a, unsigned char c) { return a == ((c == 0) ? INT_MIN : INT_MAX); } +static inline void oor_set(int *a, unsigned char c) { *a = ((c == 0) ? INT_MIN : INT_MAX); } + +DICT_OASET_DEF(dict_int, int, M_OPEXTEND(M_DEFAULT_OPLIST, OOR_EQUAL(oor_equal_p), OOR_SET(oor_set M_IPTR))) + +// Returns length of the longest contiguous subsequence +int mlib_find_longest(int arr[], int n) +{ + dict_int_t S; + int ans = 0; + + dict_int_init(S); + + // Hash all the array elements + for (int i = 0; i < n; i++) + dict_int_push(S, arr[i]); + + // check each possible sequence from + // the start then update optimal length + for (int i = 0; i < n; i++) { + // if current element is the starting + // element of a sequence + int *p = dict_int_get(S, arr[i]-1); + if (p == NULL) { + // Then check for next elements + // in the sequence + int j = arr[i] + 1; + while (dict_int_get(S, j) != NULL) + j++; + + // update optimal length if + // this length is more + ans = M_MAX(ans, j - arr[i]); + } + } + + dict_int_clear(S); + return ans; +} + +#include "common.h" + +int main(int argc, const char *argv[]) +{ + int n = argc == 1 ? 10000 : atoi(argv[1]); + int *arr = (int*) malloc(n * sizeof(int)); + for(int i = 0; i < n; i++) + arr[i] = rand_get(); + + unsigned long long t0 = cputime(); + cout << "Length of the Longest contiguous subsequence is " + << cpp_find_longest(arr, n) << endl; + + unsigned long long t1 = cputime(); + + cout << "Length of the Longest contiguous subsequence is " + << mlib_find_longest(arr, n) << endl; + unsigned long long t2 = cputime(); + + cout << "Time C++: " << (t1-t0) << " Time M*LIB: " << (t2-t1) << endl; + return 0; +}