Last active
October 21, 2018 19:28
-
-
Save bwedding/8c7926224d91ac632827ceae55a64c0d to your computer and use it in GitHub Desktop.
Testing Parallel sorting using MSVC 17 and C++17 parallel algorithms
This file contains hidden or 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
// Be sure to set your project to use the ISO C++17 Standard (/std:c++17) under | |
// C++ Language Standard in Properties... C/C++... Language... | |
#include <string> | |
#include <iostream> | |
#include <algorithm> | |
#include <execution> | |
#include <random> | |
#include <chrono> | |
#include <iomanip> | |
// All this should be in a header, but I'm doing this for ease of use in a Gist | |
// Readability | |
using TimePointRef = std::chrono::time_point<std::chrono::steady_clock> &; | |
using TimePoint = std::chrono::high_resolution_clock::time_point; | |
using DurationDblRef = std::chrono::duration<double> &; | |
using DurationDbl = std::chrono::duration<double>; | |
void GetTotalTime(DurationDblRef totalTime, TimePointRef startTime); | |
void PrintResults(char *funcName, DurationDblRef totalTime); | |
static TimePoint GetTime(void); | |
// Return time in seconds as a double. | |
static TimePoint GetTime(void) | |
{ | |
return std::chrono::high_resolution_clock::now(); | |
} | |
void TestIt() | |
{ | |
std::vector<int> vec; | |
std::vector<int> cpyVec; | |
std::random_device rd; // Will be used to obtain a seed for the random number engine | |
std::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd() | |
std::uniform_int_distribution<> dis(1, 1000); | |
DurationDbl totalTime; | |
for (int j = 0; j < 4000000; j++) | |
{ | |
vec.push_back(dis(gen)); | |
} | |
auto startTime = GetTime(); | |
for (int i = 0; i < 20; i++) | |
{ | |
cpyVec = vec; | |
std::sort(std::execution::par, cpyVec.begin(), cpyVec.end()); // parallel | |
} | |
GetTotalTime(totalTime, startTime); | |
PrintResults((char*)"parallel", totalTime); | |
startTime = GetTime(); | |
for (int i = 0; i < 20; i++) | |
{ | |
cpyVec = vec; | |
std::sort(std::execution::par_unseq, cpyVec.begin(), cpyVec.end()); // parallel and vectorized | |
} | |
GetTotalTime(totalTime, startTime); | |
PrintResults((char*)"par & vect", totalTime); | |
startTime = GetTime(); | |
for (int i = 0; i < 20; i++) | |
{ | |
cpyVec = vec; | |
std::sort(cpyVec.begin(), cpyVec.end()); // standard sort | |
} | |
GetTotalTime(totalTime, startTime); | |
PrintResults((char*)"Plane Old Sort", totalTime); | |
cpyVec = vec; | |
startTime = GetTime(); | |
for (int i = 0; i < 20; i++) | |
{ | |
cpyVec = vec; | |
std::sort(std::execution::seq, cpyVec.begin(), cpyVec.end()); // sequential | |
} | |
GetTotalTime(totalTime, startTime); | |
PrintResults((char*)"Sequential", totalTime); | |
} | |
void PrintResults(char *funcName, DurationDblRef totalTime) | |
{ | |
std::string fname(funcName); | |
fname += ":"; | |
std::cout << std::left << std::setw(15) << funcName << std::right << std::setw(12) << totalTime.count() << " Seconds\n"; | |
} | |
void GetTotalTime(DurationDblRef totalTime, TimePointRef startTime) | |
{ | |
totalTime = std::chrono::duration_cast<DurationDbl>(GetTime() - startTime); | |
} | |
int main() | |
{ | |
TestIt(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment