Performance 我的代码中是否存在使线程速度变慢的开销[C++]

Performance 我的代码中是否存在使线程速度变慢的开销[C++],performance,c++,multithreading,Performance,C++,Multithreading,我创建了两个程序来查找两个矩阵的行列式,一个使用线程,另一个不使用线程,然后记录完成计算所需的时间。线程脚本似乎比没有线程的脚本慢,但我看不到任何可能造成开销问题的东西。谢谢你的帮助 线程脚本: #include <iostream> #include <ctime> #include <thread> void determinant(int matrix[3][3]){ int a = matrix[0][0]*((matrix[1][1]*matr

我创建了两个程序来查找两个矩阵的行列式,一个使用线程,另一个不使用线程,然后记录完成计算所需的时间。线程脚本似乎比没有线程的脚本慢,但我看不到任何可能造成开销问题的东西。谢谢你的帮助

线程脚本:

#include <iostream>
#include <ctime>
#include <thread>

void determinant(int matrix[3][3]){
  int a = matrix[0][0]*((matrix[1][1]*matrix[2][2])-(matrix[1][2]*matrix[2][1]));
  int b = matrix[0][1]*((matrix[1][0]*matrix[2][2])-(matrix[1][2]*matrix[2][0]));
  int c = matrix[0][2]*((matrix[1][0]*matrix[2][1])-(matrix[1][1]*matrix[2][0]));
  int determinant = a-b+c;
}

int main() {
  int matrix[3][3]= {
    {11453, 14515, 1399954}, 
    {13152, 11254, 11523}, 
    {11539994, 51821, 19515}
  };
  int matrix2[3][3] = {
    {16392, 16999942, 18682}, 
    {5669, 466999832, 1429}, 
    {96989, 10962, 63413}
  };
  const clock_t c_start = clock();
  std::thread mat_thread1(determinant, matrix);
  std::thread mat_thread2(determinant, matrix2);
  mat_thread1.join();
  mat_thread2.join();
  const clock_t c_end = clock();
  std::cout << "\nOperation takes: " << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << "ms of CPU time";
}
除主线程外没有其他线程的脚本:

#include <iostream>
#include <ctime>
#include <thread>

void determinant(int matrix[3][3]){
  int a = matrix[0][0]*((matrix[1][1]*matrix[2][2])-(matrix[1][2]*matrix[2][1]));
  int b = matrix[0][1]*((matrix[1][0]*matrix[2][2])-(matrix[1][2]*matrix[2][0]));
  int c = matrix[0][2]*((matrix[1][0]*matrix[2][1])-(matrix[1][1]*matrix[2][0]));
  int determinant = a-b+c;
}

int main() {
  int matrix[3][3]= {
    {11453, 14515, 1399954}, 
    {13152, 11254, 11523}, 
    {11539994, 51821, 19515}
  };
  int matrix2[3][3] = {
    {16392, 16999942, 18682}, 
    {5669, 466999832, 1429}, 
    {96989, 10962, 63413}
  };
  const clock_t c_start = clock();
  determinant(matrix);
  determinant(matrix2);
  const clock_t c_end = clock();
  std::cout << "\nOperation takes: " << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << "ms of CPU time";
}
PS-第一个脚本在最后一次运行时耗时0.293ms,第二个脚本耗时0.002ms

再次感谢


wndlbh

区别似乎在于创建了两个线程和连接。我希望创建和连接的时间远远超过9次乘法和5次加法的时间。

区别似乎在于创建了两个线程和连接。我希望创建和连接的时间远远超过9次乘法和5次加法的时间。

新线程的启动和拆卸成本是巨大的,在这种情况下,淹没了真正的工作。
我似乎记得1到1秒之间的时间,这取决于你的设置。如果工作中节省的时间高于创建线程的成本,那么多线程首先会有所帮助。在这种情况下,您需要1000次计算才能节省这么多。

新线程的启动和拆卸成本是巨大的,在这种情况下,实际工作就被淹没了。
我似乎记得1到1秒之间的时间,这取决于你的设置。如果工作中节省的时间高于创建线程的成本,那么多线程首先会有所帮助。在这种情况下,您需要1000次计算才能节省这么多。

这似乎是关于编程的问题。这不是关于Unix的问题。我不确定问题是在代码中还是在实际硬件中,即使用taskset将脚本强制加载到某个cpu上,使代码按预期工作。这似乎是关于编程的问题。这不是关于Unix的问题。我不确定问题是在代码中还是在实际硬件中,即使用taskset将脚本强制加载到某个cpu上,使代码按预期工作。谢谢。线程不应该并发运行吗?线程不应该并发运行吗?是的,但是创建线程比完成工作需要更多的时间,因此单线程代码会更快。你必须用一个更大的矩阵来测试你的代码,才能看到多线程代码的成功。谢谢你,我必须试试这个,看看动作的不同之处谢谢。线程不应该并发运行吗?线程不应该并发运行吗?是的,但是创建线程比完成工作需要更多的时间,因此单线程代码会更快。你必须用一个更大的矩阵来测试你的代码,才能看到多线程代码的成功。谢谢你,我将不得不尝试一下,看看它们之间的区别