C++ qt4 QT并发性能问题

C++ qt4 QT并发性能问题,c++,multithreading,qt,memory,cpu,C++,Multithreading,Qt,Memory,Cpu,我正在尝试使用QtConcurrent将函数调用拆分为4个不同的线程。我的代码的简化版本: void Myclass someFunction(){ int ct=0; for ( int i = -500000; i < 500000; i+=4) if(multiThreadingEnabled){ QtConcurrent::run(this,&Myclass::heavyCalc, i, ct); QtConcu

我正在尝试使用QtConcurrent将函数调用拆分为4个不同的线程。我的代码的简化版本:

void Myclass someFunction(){

int ct=0;
for ( int i = -500000; i < 500000; i+=4)
      if(multiThreadingEnabled){
            QtConcurrent::run(this,&Myclass::heavyCalc, i, ct);
            QtConcurrent::run(this,&Myclass::heavyCalc, i+1, ct+1);
            QtConcurrent::run(this,&Myclass::heavyCalc, i+2, ct+2);
            QtConcurrent::run(this,&Myclass::heavyCalc, i+3, ct+3);
            }
            else {...}
            ct+=4;
....
}

void Mygraph::heavyCalc(int,int){
....
      resArr[ct][0]=i;
      resArr[ct][1]=operation(i);
....
}
void Myclass someFunction(){
int-ct=0;
对于(int i=-500000;i<500000;i+=4)
if(可多线程){
QtConcurrent::run(this,&Myclass::heavyCalc,i,ct);
QtConcurrent::run(this,&Myclass::heavyCalc,i+1,ct+1);
QtConcurrent::run(this,&Myclass::heavyCalc,i+2,ct+2);
QtConcurrent::run(this,&Myclass::heavyCalc,i+3,ct+3);
}
否则{…}
ct+=4;
....
}
void Mygraph::heavyCalc(int,int){
....
resArr[ct][0]=i;
resArr[ct][1]=操作(i);
....
}
其思想是使用所有4个核(工作)来拥有4个线程。没有线程,仅使用1个内核。我预计计算速度会快4倍,但这相当于+10-20%

  • 多次调用QTL是否效率低下/是否有更好的方法

  • 内存是否降低了计算速度(结果存储在一个非常大的数组中)

  • 当使用4个核而不是1个核时,期望计算速度提高4倍,这通常是错误的吗


  • 创建新线程时,会有一些开销(用于分配和启动线程,用于将QFutureWatcher添加到队列中,用于QtConcurrent),线程工作时也会有一些开销。在您的代码中创建250000个任务,这个数字似乎太大了。。。(当然,对于我来说,如果代码<某个函数< /代码>是重复调用的,你可以考虑使用它,这可能有助于在<代码> MyGrase<代码>的构造上生成一个线程池,并在那里发布<代码> HyryCalc < /C>