-
-
Save Light-City/3c03840eaaa9f327602cd4e27c4f6bec to your computer and use it in GitHub Desktop.
sort parallel with STL and TBB.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <cassert> | |
#include <chrono> | |
#include <execution> | |
#include <random> | |
#include <iostream> | |
#include <vector> | |
#include <benchmark/benchmark.h> | |
// Function to initialize random data | |
std::vector<unsigned long long> initializeRandomData(unsigned long long size) { | |
std::vector<unsigned long long> data; | |
std::mt19937 prng(std::random_device{}()); | |
std::uniform_int_distribution<uint64_t> zero_ull_max(0); | |
for (unsigned long long i = 0; i < size; ++i) { | |
data.push_back(zero_ull_max(prng)); | |
} | |
return data; | |
} | |
// static void BM_ParallelUnSeqSort(benchmark::State& state) { | |
// std::vector<unsigned long long> input_parallel; | |
// for (auto _ : state) { | |
// input_parallel = initializeRandomData(state.range(0)); | |
// auto start = std::chrono::high_resolution_clock::now(); | |
// std::sort(std::execution::par_unseq, input_parallel.begin(), input_parallel.end()); | |
// auto end = std::chrono::high_resolution_clock::now(); | |
// state.SetIterationTime(std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count() * 1e-9); | |
// } | |
// } | |
// BENCHMARK(BM_ParallelUnSeqSort)->Arg(16384)->Arg(16384000)->Arg(163840000); | |
// Benchmark function for parallel sorting | |
static void BM_ParallelSort(benchmark::State& state) { | |
std::vector<unsigned long long> input_parallel; | |
for (auto _ : state) { | |
input_parallel = initializeRandomData(state.range(0)); | |
auto start = std::chrono::high_resolution_clock::now(); | |
std::sort(std::execution::par, input_parallel.begin(), input_parallel.end()); | |
auto end = std::chrono::high_resolution_clock::now(); | |
state.SetIterationTime(std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count() * 1e-9); | |
} | |
} | |
BENCHMARK(BM_ParallelSort)->Arg(16384)->Arg(16384000)->Arg(163840000); | |
// Benchmark function for serial sorting | |
static void BM_SerialSort(benchmark::State& state) { | |
std::vector<unsigned long long> input_serial; | |
for (auto _ : state) { | |
input_serial = initializeRandomData(state.range(0)); | |
auto start = std::chrono::high_resolution_clock::now(); | |
std::sort(std::execution::seq, input_serial.begin(), input_serial.end()); | |
auto end = std::chrono::high_resolution_clock::now(); | |
state.SetIterationTime(std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count() * 1e-9); | |
} | |
} | |
BENCHMARK(BM_SerialSort)->Arg(16384)->Arg(16384000)->Arg(163840000); | |
BENCHMARK_MAIN(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment