gpt4 book ai didi

c++ - 矩阵的乘法。表现

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:13:44 28 4
gpt4 key购买 nike

<分区>

我有一个方阵乘法程序。另外,我认为程序的性能由公式(操作数)/(运行时间)决定。为什么矩阵的增长维度会降低性能?谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sys/time.h>
using namespace std;

double getsec(){
struct timeval t;
gettimeofday(&t,NULL);
return t.tv_sec+t.tv_usec*0.000001;
}

int main(int argc, char* argv[])
{
double begintime=getsec();

int n;
if(argc==2)n=atoi(argv[1]);
else n=3;

int**a=new int*[n];
double**b=new double*[n];
double**c=new double*[n];
for (int i=0;i<n;i++){
a[i]=new int [n];
b[i]=new double [n];
c[i]=new double [n];
}

for (int i=0;i<n;i++)
for(int j=0;j<n;j++){
a[i][j]=i+1;
b[i][j]=1/(j+1.);
c[i][j]=0;
}

for (int i=0;i<n;i++)
for(int j=0;j<n;j++)
for(int k=0;k<n;k++)
c[i][j]+=a[i][k]*b[k][j];

double qty_of_operations = (double)2*n*n*n;

cout<<n<<" c11="<<c[0][0]<<" c1n="<<c[0][n-1]<<" cn1="<<c[n-1][0]<<" cnn="<<c[n-1][n-1]<<" "<<qty_of_operations/(getsec()-begintime)<<endl;
return 0;

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com