LINUX.ORG.RU

История изменений

Исправление arax, (текущая версия) :

Вроде как тут и есть намного быстрее

Нет намного быстрее это так:

~/Documents/cpp ❯ g++ -ltbb -O2 -march=native -o testThreads ./test.cpp && ./testThreads
32 threads
93ms
664579 prime count

#include <iostream>
#include <cmath>
#include <algorithm>
#include <execution>
#include <vector>
#include <chrono>
#include <thread>


using namespace std;

bool isPrime(int num)
{
    if (num == 2) {
        return true;
    }
    if (num <= 1 || num % 2 == 0) {
        return false;
    }

    double sqrt_num = sqrt(double(num));
    for (int div = 3; div <= sqrt_num; div +=2)
    {
        if (num % div == 0) {
            return false;
        }
    }
    return true;
}



int main()
{
    int N = 10000000;

    int num_phreads = std::thread::hardware_concurrency();
    std::cout << num_phreads << " threads\n";

    std::vector<int> it(num_phreads),res(num_phreads,0);
    std::iota (std::begin(it), std::end(it), 0);
    int K = N / num_phreads;
    auto begin = std::chrono::high_resolution_clock::now();

    std::for_each(std::execution::par_unseq, std::begin(it), std::end(it), [&](int j) {
    int start_num= j*K;
    int end_num = start_num+K;
    for (int i = start_num; i < end_num; i++) {
            if(isPrime(i)){
                res[j]++;
            }
        }
    });

    auto end = std::chrono::high_resolution_clock::now();
    std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end-begin).count() << "ms" << std::endl;

    cout << std::reduce(res.begin(), res.end());;
    cout << " prime count\n";
    return 0;
}

Исходная версия arax, :

Вроде как тут и есть намного быстрее

Нет намного быстрее это так:

~/Documents/cpp ❯ g++ -ltbb -O2 -march=native -o testThreads ./test.cpp && ./testThreads
32 threads
93ms
664579 prime count

#include <iostream>
#include <cmath>
#include <algorithm>
#include <execution>
#include <vector>
#include <chrono>
#include <thread>


using namespace std;

bool isPrime(int num)
{
    if (num == 2) {
        return true;
    }
    if (num <= 1 || num % 2 == 0) {
        return false;
    }

    double sqrt_num = sqrt(double(num));
    for (int div = 3; div <= sqrt_num; div +=2)
    {
        if (num % div == 0) {
            return false;
        }
    }
    return true;
}



int main()
{
    int N = 10000000;

    int num_phreads = std::thread::hardware_concurrency();
    std::cout << num_phreads << " threads\n";

    std::vector<int> it(num_phreads),res(num_phreads,0);
    std::iota (std::begin(it), std::end(it), 0);
    int K = N / num_phreads;
    auto begin = std::chrono::high_resolution_clock::now();

    std::for_each(std::execution::par_unseq, std::begin(it), std::end(it), [&](int j) {
    int start_num= j*K;
    int end_num = s+K;
    for (int i = start_num; i < end_num; i++) {
            if(isPrime(i)){
                res[j]++;
            }
        }
    });

    auto end = std::chrono::high_resolution_clock::now();
    std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end-begin).count() << "ms" << std::endl;

    cout << std::reduce(res.begin(), res.end());;
    cout << " prime count\n";
    return 0;
}