C++17 multi-core algorithm small test

咳咳~C++11 even did not get through it C++17 is coming again, this is how we stick to the traditional C++ how to live TT

first come Single-core look:

#include <stddef.h>
#include <stdio.h>
#include <algorithm>
#include <chrono>
#include <random>
#include <ratio>
#include <vector>
 
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
using std::milli;
using std::random_device;
using std::sort;
using std::vector;
 
const size_t testSize = 1'000'000;
const int iterationCount = 5;
 
void print_results(const char *const tag, const vector<double>& sorted,
                   high_resolution_clock::time_point startTime,
                   high_resolution_clock::time_point endTime) {
  printf("%s: Lowest: %g Highest: %g Time: %fms\n", tag, sorted.front(),
         sorted.back(),
         duration_cast<duration<double, milli>>(endTime - startTime).count());
}
 
int main() {
  random_device rd;
 
  // generate some random doubles:
  printf("Testing with %zu doubles...\n", testSize);
  vector<double> doubles(testSize);
  for (auto& d : doubles) {
    d = static_cast<double>(rd());
  }
 
  // time how long it takes to sort them:
  for (int i = 0; i < iterationCount; ++i)
  {
    vector<double> sorted(doubles);
    const auto startTime = high_resolution_clock::now();
    sort(sorted.begin(), sorted.end());
    const auto endTime = high_resolution_clock::now();
    print_results("Serial", sorted, startTime, endTime);
  }
}

Debug Results:

咳咳~ My 3612 notebook is really not working? The desktop of the company 7700 is more than 200. The result of

Release:

looks like an order of magnitude faster! !

Next is the multi-core version:

// C17P.cpp : This file contains the "main" function. Program execution will start and end here.
//

#include "pch.h"
#include <iostream>

#include "pch.h"
#include <iostream>

#include <stddef.h>
#include <stdio.h>
#include <algorithm>
#include <chrono>
#include <random>
#include <ratio>
#include <vector>
#include <execution>

Using std::chrono::duration;
Using std::chrono::duration_cast;
Using std::chrono::high_resolution_clock;
Using std::milli;
Using std::random_device;
Using std::sort;
Using std::vector;
Using std::execution;

Const size_t testSize = 1'000'000;
Const int iterationCount = 5;

Void print_results(const char *const tag, const vector<double>& sorted,
High_resolution_clock::time_point startTime,
High_resolution_clock::time_point endTime) {
Printf("%s: Lowest: %g Highest: %g Time: %fms\n", tag, sorted.front(),
Sorted.back(),
Duration_cast<duration<double, milli>>(endTime - startTime).count());
}

Int main() {
Random_device rd;

// generate some random doubles:
Printf("Testing with %zu doubles...\n", testSize);
Vector<double> doubles(testSize);
For (auto& d : doubles) {
d = static_cast<double>(rd());
}

// time how long it takes to sort them:
For (int i = 0; i < iterationCount; ++i)
{
Vector<double> sorted(doubles);
Const auto startTime = high_resolution_clock::now();
// same sort call as above, but with par_unseq:
Sort(std::execution::par_unseq, sorted.begin(), sorted.end());
Const auto endTime = high_resolution_clock::now();
// in our output, note that these are the parallel results:
Print_results("Parallel", sorted, startTime, endTime);
}
}

// Run the program: Ctrl + F5 or Debug > Start Execution (Do Not Debug) menu
// Debugger: F5 or Debug > Start Debugging menu

// Getting Started Tip:
// 1. Add/manage files using the Solution Explorer window
// 2. Connect to source control using the Team Explorer window
// 3. Use the output window to view the generated output and other messages
// 4. Use the error list window to view the error
// 5. Go to Project > Add New Item to create a new code file, or go to Project > Add Existing Item to add an existing code file to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file 

to compile and get the result:

嗯嗯~多The kernel algorithm only supports C++17.

I am too lazy to use Baidu VS2017 how to use C++17, use the method of foreigners, open a 2017 tool window, enter

cl /EHsc /W4 /WX /std:c++latest /Fedebug /MDd .\program.cpp

Tell me

好吧, can not use std: :execution.

will be

//using std::execution;

shielding 26 lines, then come:

嗯~ compiled through. Try it out:

See it, debug is the effect of release! ! !