|
| 1 | +#include <cmath> |
| 2 | + |
| 3 | +static constexpr char x[] = "x"; |
| 4 | +static constexpr char euclidean[] = "euclidean"; |
| 5 | +static constexpr char taxicab[] = "taxicab"; |
| 6 | +static constexpr char chebyshev[] = "chebyshev"; |
| 7 | + |
| 8 | +template <const char* val> |
| 9 | +struct Point3D { |
| 10 | + double x; |
| 11 | + double y; |
| 12 | + double z; |
| 13 | + static constexpr std::string_view name {val}; |
| 14 | + Point3D() |
| 15 | + { |
| 16 | + x = (double)rand() / RAND_MAX; |
| 17 | + y = (double)rand() / RAND_MAX; |
| 18 | + z = (double)rand() / RAND_MAX; |
| 19 | + } |
| 20 | + double distance() |
| 21 | + { |
| 22 | + if constexpr (name == "x") { |
| 23 | + return x; |
| 24 | + } |
| 25 | + else if constexpr (name == "euclidean") { |
| 26 | + return std::sqrt(x * x + y * y + z * z); |
| 27 | + } |
| 28 | + else if constexpr (name == "taxicab") { |
| 29 | + return abs(x) + abs(y) + abs(z); |
| 30 | + } |
| 31 | + else if constexpr (name == "chebyshev") { |
| 32 | + return std::max(std::max(x, y), z); |
| 33 | + } |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +template <typename T> |
| 38 | +std::vector<T> init_data(const int size) |
| 39 | +{ |
| 40 | + srand(42); |
| 41 | + std::vector<T> arr; |
| 42 | + for (auto ii = 0; ii < size; ++ii) { |
| 43 | + T temp; |
| 44 | + arr.push_back(temp); |
| 45 | + } |
| 46 | + return arr; |
| 47 | +} |
| 48 | + |
| 49 | +template <typename T> |
| 50 | +struct less_than_key { |
| 51 | + inline bool operator()(T &p1, T &p2) |
| 52 | + { |
| 53 | + return (p1.distance() < p2.distance()); |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +template <typename T> |
| 58 | +static void scalarobjsort(benchmark::State &state) |
| 59 | +{ |
| 60 | + // set up array |
| 61 | + std::vector<T> arr = init_data<T>(state.range(0)); |
| 62 | + std::vector<T> arr_bkp = arr; |
| 63 | + // benchmark |
| 64 | + for (auto _ : state) { |
| 65 | + std::sort(arr.begin(), arr.end(), less_than_key<T>()); |
| 66 | + state.PauseTiming(); |
| 67 | + arr = arr_bkp; |
| 68 | + state.ResumeTiming(); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +template <typename T> |
| 73 | +static void simdobjsort(benchmark::State &state) |
| 74 | +{ |
| 75 | + // set up array |
| 76 | + std::vector<T> arr = init_data<T>(state.range(0)); |
| 77 | + std::vector<T> arr_bkp = arr; |
| 78 | + // benchmark |
| 79 | + for (auto _ : state) { |
| 80 | + x86simdsort::object_qsort(arr.data(), arr.size(), [](T p) -> double { |
| 81 | + return p.distance(); |
| 82 | + }); |
| 83 | + state.PauseTiming(); |
| 84 | + if (!std::is_sorted(arr.begin(), arr.end(), less_than_key<T>())) { |
| 85 | + std::cout << "sorting failed \n"; |
| 86 | + } |
| 87 | + arr = arr_bkp; |
| 88 | + state.ResumeTiming(); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#define BENCHMARK_OBJSORT(func, T) \ |
| 93 | + BENCHMARK_TEMPLATE(func, T) \ |
| 94 | + ->Arg(10e1) \ |
| 95 | + ->Arg(10e2) \ |
| 96 | + ->Arg(10e3) \ |
| 97 | + ->Arg(10e4) \ |
| 98 | + ->Arg(10e5) \ |
| 99 | + ->Arg(10e6); |
| 100 | + |
| 101 | +BENCHMARK_OBJSORT(simdobjsort, Point3D<x>) |
| 102 | +BENCHMARK_OBJSORT(scalarobjsort, Point3D<x>) |
| 103 | +BENCHMARK_OBJSORT(simdobjsort, Point3D<taxicab>) |
| 104 | +BENCHMARK_OBJSORT(scalarobjsort, Point3D<taxicab>) |
| 105 | +BENCHMARK_OBJSORT(simdobjsort, Point3D<euclidean>) |
| 106 | +BENCHMARK_OBJSORT(scalarobjsort, Point3D<euclidean>) |
| 107 | +BENCHMARK_OBJSORT(simdobjsort, Point3D<chebyshev>) |
| 108 | +BENCHMARK_OBJSORT(scalarobjsort, Point3D<chebyshev>) |
0 commit comments