Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;多线程性能比单线程代码慢 我在C++中学习使用线程 我用整数创建了一个很长的向量,并设置了另一个整数x。我想计算这个整数和向量中的整数之间的差 但是,在我的实现中,使用两个线程的函数比使用单线程的函数慢。我想知道原因是什么,我如何才能正确地实现线程,使它运行得更快 代码如下: #include <iostream> #include <vector> #include <thread> #include <future> #include <math.h> using namespace std; vector<int> vector_generator(int size) { vector<int> temp; for (int i = 0; i < size; i++) { temp.push_back(i); } return temp; } vector<int> dist_calculation(int center, vector<int> &input, int start, int end) { vector<int> temp; for (int i = start; i < end; i++) { temp.push_back(abs(center - input[i])); } return temp; } void multi_dist_calculation(int center, vector<int> &input) { int mid = input.size() / 2; vector<int> temp1(input.begin(), input.begin() + mid); vector<int> temp2(input.begin()+mid, input.end()); auto future1 = async(dist_calculation, center, temp1, 0, mid); auto future2 = async(dist_calculation, center, temp2, 0, mid); vector<int> result1 = future1.get(); vector<int> result2 = future2.get(); return; } int main() { vector<int> v1 = vector_generator(1000000000); vector<int> result; multi_dist_calculation(0, v1); //dist_calculation(50, v1, 0, v1.size()); return 0; } #包括 #包括 #包括 #包括 #包括 使用名称空间std; 向量发生器(整数大小){ 向量温度; 对于(int i=0;i_C++_Multithreading - Fatal编程技术网

C++;多线程性能比单线程代码慢 我在C++中学习使用线程 我用整数创建了一个很长的向量,并设置了另一个整数x。我想计算这个整数和向量中的整数之间的差 但是,在我的实现中,使用两个线程的函数比使用单线程的函数慢。我想知道原因是什么,我如何才能正确地实现线程,使它运行得更快 代码如下: #include <iostream> #include <vector> #include <thread> #include <future> #include <math.h> using namespace std; vector<int> vector_generator(int size) { vector<int> temp; for (int i = 0; i < size; i++) { temp.push_back(i); } return temp; } vector<int> dist_calculation(int center, vector<int> &input, int start, int end) { vector<int> temp; for (int i = start; i < end; i++) { temp.push_back(abs(center - input[i])); } return temp; } void multi_dist_calculation(int center, vector<int> &input) { int mid = input.size() / 2; vector<int> temp1(input.begin(), input.begin() + mid); vector<int> temp2(input.begin()+mid, input.end()); auto future1 = async(dist_calculation, center, temp1, 0, mid); auto future2 = async(dist_calculation, center, temp2, 0, mid); vector<int> result1 = future1.get(); vector<int> result2 = future2.get(); return; } int main() { vector<int> v1 = vector_generator(1000000000); vector<int> result; multi_dist_calculation(0, v1); //dist_calculation(50, v1, 0, v1.size()); return 0; } #包括 #包括 #包括 #包括 #包括 使用名称空间std; 向量发生器(整数大小){ 向量温度; 对于(int i=0;i

C++;多线程性能比单线程代码慢 我在C++中学习使用线程 我用整数创建了一个很长的向量,并设置了另一个整数x。我想计算这个整数和向量中的整数之间的差 但是,在我的实现中,使用两个线程的函数比使用单线程的函数慢。我想知道原因是什么,我如何才能正确地实现线程,使它运行得更快 代码如下: #include <iostream> #include <vector> #include <thread> #include <future> #include <math.h> using namespace std; vector<int> vector_generator(int size) { vector<int> temp; for (int i = 0; i < size; i++) { temp.push_back(i); } return temp; } vector<int> dist_calculation(int center, vector<int> &input, int start, int end) { vector<int> temp; for (int i = start; i < end; i++) { temp.push_back(abs(center - input[i])); } return temp; } void multi_dist_calculation(int center, vector<int> &input) { int mid = input.size() / 2; vector<int> temp1(input.begin(), input.begin() + mid); vector<int> temp2(input.begin()+mid, input.end()); auto future1 = async(dist_calculation, center, temp1, 0, mid); auto future2 = async(dist_calculation, center, temp2, 0, mid); vector<int> result1 = future1.get(); vector<int> result2 = future2.get(); return; } int main() { vector<int> v1 = vector_generator(1000000000); vector<int> result; multi_dist_calculation(0, v1); //dist_calculation(50, v1, 0, v1.size()); return 0; } #包括 #包括 #包括 #包括 #包括 使用名称空间std; 向量发生器(整数大小){ 向量温度; 对于(int i=0;i,c++,multithreading,C++,Multithreading,更新#1 添加了std::launch::async&reserve()的建议,它确实使代码更快。但是双线程函数仍然比单线程函数慢。我能说在这种计算中,单线程更快吗 #include <iostream> #include <vector> #include <thread> #include <future> #include <math.h> using namespace std; vector<int> vec



更新#1

添加了std::launch::async&reserve()的建议,它确实使代码更快。但是双线程函数仍然比单线程函数慢。我能说在这种计算中,单线程更快吗

#include <iostream>
#include <vector>
#include <thread>
#include <future>
#include <math.h>

using namespace std;


vector<int> vector_generator(int size) {
    vector<int> temp;
    temp.reserve(size);
    for (int i = 0; i < size; i++) {
        temp.push_back(i);
    }
    return temp;
}

vector<int> dist_calculation(int center, vector<int> &input, int start, int end) {
    vector<int> temp;
    temp.reserve(end - start);
    for (int i = start; i < end; i++) {
        temp.push_back(abs(center - input[i]));
    }
    return temp;
}


void multi_dist_calculation(int center, vector<int> &input) {
    int mid = input.size() / 2;

    auto future1 = async(std::launch::async, dist_calculation, center, input,   0, mid);
    auto future2 = async(std::launch::async, dist_calculation, center, input, mid, input.size());

    vector<int> result1 = future1.get();
    vector<int> result2 = future2.get();

    return;
}


int main() {

    vector<int> v1 = vector_generator(1000000000);
    vector<int> result;
    int center = 0;
    multi_dist_calculation(center, v1);
    //dist_calculation(center, v1, 0, v1.size());

    return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
向量发生器(整数大小){
向量温度;
温度储备(尺寸);
对于(int i=0;i
您没有将任何
std::launch policy
传递给,因此它给实现留下了很大的自由

行为就像调用(2)时策略为std::launch::async | std::launch::deferred。换句话说,f可以在另一个线程中执行,或者当查询结果std::future以获取值时,它可以同步运行

但也要注意,一般来说,使用更多线程,特别是在小任务上,可能不会更快

  • 如果
    dist_calculation
    或任何要执行的任务都是少量工作,请注意开销。创建一个新线程的成本相对较高,而且无论内部池
    std::async
    使用什么、承诺什么和未来,都会有开销
  • 此外,按照编写的方式,您可能会创建更多向量,具有更多动态内存,并且需要合并结果,这也会带来一些成本
  • 在更复杂的情况下,如果涉及同步,例如使用
    std::mutex
    ,则可能会比额外线程获得的性能代价更高
  • 在某些情况下,瓶颈不会是CPU。例如,可能是磁盘/存储速度(包括页面/交换文件)、网络速度(包括远程服务器),甚至是内存带宽(除了支持NUMA的优化,它们比仅使用
    std::async
    )复杂得多)。其中的多线程只会增加开销,但没有任何好处
您应该在可能的情况下首先使用其他基本优化,例如
保留
向量的大小以避免不必要的分配和复制,可能
调整
的大小并使用
向量[索引]=a
而不是
推回
,等等


对于像
abs(中心输入[i])
这样简单的东西,单指令多数据(SIMD)优化可能会带来更多的改进。e、 g.确保您在编译时启用了任何优化,例如
SSE2
,如果编译器没有适当地优化循环(我认为
push_back
可能会干扰,请测试!),请稍微更改它,或者甚至可以显式地使用向量指令(对于x86,请签出
\u mm\u add\u epi32
等).

如果我理解正确,那么您的单线程版本只需对给定向量调用
dist\u计算
,它将遍历该向量一次,并生成一个新的向量并返回。另一方面,多线程版本首先将输入数据的每一半复制到两个单独的向量。然后,它为每一半启动
dist_计算
,可能是两个线程。可能甚至无法使用单独的线程运行,因为您没有指定启动策略。如果它碰巧使用单独的线程运行,那么由于构造函数的工作方式,您传递的向量将再次被复制。您必须使用,例如,正确传递引用。想想那里发生了什么。将数据复制到这两个向量意味着您之前已经对整个数据运行了一次
template <typename ExecutionPolicy, typename ForwardIterator, typename OutputIterator>
void multi_dist_calculation(OutputIterator dest, ForwardIterator begin, ForwardIterator end, int center, ExecutionPolicy&& execution_policy)
{
    std::transform(std::forward<ExecutionPolicy>(execution_policy), begin, end, dest, [center](int x)
    {
        return std::abs(center - x);
    });
}

int main()
{
    constexpr int data_size = 1000000000;
    auto data = std::unique_ptr<int[]> { new int[data_size] };
    std::iota(&data[0], &data[0] + data_size, 0);

    auto result = std::unique_ptr<int[]> { new int[data_size] };
    multi_dist_calculation(&result[0], &data[0], &data[0] + data_size, 0, std::execution::par_unseq);  // multithreaded
    // multi_dist_calculation(&result[0], &data[0], &data[0] + data_size, 0, std::execution::seq);  // singlethreaded

    return 0;
}