C++;vs python numpy复杂阵列性能
有人能告诉我为什么这两个程序在运行时间上有巨大的差异吗?我只是简单地把两个大型的复杂数组相乘,比较Python(NoMpy)和C++的时间。我用-O3标志用G++编译这个C++代码。我发现,只有当我在C++中使用<强>复杂的< /强>浮点时,巨大的差异才出现,它在NUMPY中的速度快了20倍。 python代码:C++;vs python numpy复杂阵列性能,python,c++,numpy,Python,C++,Numpy,有人能告诉我为什么这两个程序在运行时间上有巨大的差异吗?我只是简单地把两个大型的复杂数组相乘,比较Python(NoMpy)和C++的时间。我用-O3标志用G++编译这个C++代码。我发现,只有当我在C++中使用复杂的< /强>浮点时,巨大的差异才出现,它在NUMPY中的速度快了20倍。 python代码: import numpy as np import time if __name__ == "__main__": # check the data type is the s
import numpy as np
import time
if __name__ == "__main__":
# check the data type is the same
a = np.zeros((1), dtype=np.complex128)
a[0] = np.complex(3.4e38,3.5e38)
print(a)
b = np.zeros((1), dtype=np.complex64)
b[0] = np.complex(3.4e38,3.5e38)
print(b) # imaginary part is infinity
length = 5000;
A = np.ones((length), dtype=np.complex64) * np.complex(1,1)
B = np.ones((length), dtype=np.complex64) * np.complex(1,0)
num_iterations = 1000000
time1 = time.time()
for _ in range(num_iterations):
A *= B
time2 = time.time()
duration = ((time2 - time1)*1e6)/num_iterations
print(duration)
C++代码:
#include <iostream>
#include <complex>
#include <chrono>
using namespace std::chrono;
using namespace std;
int main()
{
// check the data type is the same
complex<double> a = complex<double>(3.4e38, 3.5e38);
cout << a << endl;
complex<float> b = complex<float>(3.4e38, 3.5e38);
cout << b << endl; // imaginary part is infinity
const int length = 5000;
static complex<float> A[length];
static complex<float> B[length];
for(int i=0; i < length; i++) {
A[i] = complex<float>(1,1);
B[i] = complex<float>(1,0);
}
int num_iterations = 1000000;
auto time1 = high_resolution_clock::now();
for(int k=0; k < num_iterations; k++)
for(int i=0; i < length; i++)
A[i] *= B[i];
auto time2 = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(time2 - time1);
cout << "average time:" << duration.count() / num_iterations << endl;
}
#包括
#包括
#包括
使用名称空间std::chrono;
使用名称空间std;
int main()
{
//检查数据类型是否相同
复合物a=复合物(3.4e38,3.5e38);
CUT< P> C++编译器为你做一些额外的检查体操,以便正确地处理NANS和其他这样的“标准”行为。
如果您添加-ffast math
优化标志,您将获得更正常的速度,但“标准”行为更少。例如复杂(inf,0)*复杂(inf,0)
不会被评估为复杂(inf,0)
。您真的在乎吗
e、 g.在最新的g++版本之前,除非使用-ffast math
,否则后面的函数速度要快得多
complex<float> mul1( complex<float> a,complex<float> b)
{
return a*b;
}
complex<float> mul2( complex<float> a,complex<float> b)
{
float * fa = reinterpret_cast<float*>(&a);
const float * fb = reinterpret_cast<float*>(&b);
float cr = fa[0]*fb[0] - fa[1]*fb[1];
float ci = fa[0]*fb[1] + fa[1]*fb[0];
return complex<float>(cr,ci);
}
复合物mul1(复合物a、复合物b)
{
返回a*b;
}
复合物mul2(复合物a、复合物b)
{
float*fa=重新解释铸件(&a);
常量浮点*fb=重新解释铸件(&b);
浮点数cr=fa[0]*fb[0]-fa[1]*fb[1];
浮动ci=fa[0]*fb[1]+fa[1]*fb[0];
返回复合物(cr,ci);
}
您可以对程序集输出以及前一个函数如何默认调用\uuuu mulsc3
S准备对C++标准关于“代码> STD::复杂< /Cord>”的另一波愤怒表示什么?你能猜出默认如何实现吗?玩吧。跟着链接,花十秒的时间思考它。< /P>
扰流器:它可能是使用一个Sqt,然后平方。< C++实现的版本是使用<代码>浮点通常是32位。你的Python使用64位复数。试试使用<代码> STD::复杂< < /代码>。如果你把数组容量作为<代码> const ,你可以做一些事情。这是:
static std::complex A[length];
与数组B
相同。您的delete
是错误的。因为您分配了一个数组,所以语法应该是delete[]一个,类似于 B。在计时周期中,您需要多次运行测试。C++时钟的分辨率不够,不能执行一次。而且,计时不准确,也不够一致。我建议至少运行1e06次测试。这会允许定时错误以及其他错误。d(例如可能的任务切换)。在internet上搜索“C++基准测试”更多关于精确分析或基准测试的信息。谢谢托马斯,我根据你的评论改变了代码,但是大的性能差异仍然存在。Nuffy Office 64的数量与C++的复杂度相当,因为这两个是两个32位浮点的组合,如代码所示。超过1000次迭代。