我的MTL矩阵乘法为什么比Java还慢?
我不懂MTL,为了做课题写计算程序评估了一下,结果出来的速度比我用Java的Colt包还慢。我相信这是我自己程序的问题,但不知道问题出在那里。
请高手指点,谢谢。
目的:矩阵与向量的积
实现一:matrix与dense1D
#include "stdafx.h"
#include "mtl/matrix.h"
#include "mtl/dense1D.h"
#include "mtl/mtl.h"
#include <atltime.h>
using namespace ATL;
using namespace std;
using namespace mtl;
typedef matrix<double, rectangle<>, dense<>,row_major>::type Matrix;
void Mytest()
{
const Matrix::size_type MAX_ROW = 100, MAX_COL = 100, TestA_ROW =1 , TestA_COL =100;
Matrix TestArray(MAX_ROW,MAX_COL);
dense1D<double> TestA(TestA_COL), Result(TestA_COL);
for (Matrix::size_type i=0; i<MAX_ROW; ++i)
{
for (Matrix::size_type j=0; j<MAX_COL; ++j)
{
TestArray(i, j) = Matrix::value_type(j);
}
}
Matrix::size_type p;
for (p = 0; p < TestA.size(); p++)
TestA[p] = p;
CTime BeginTime = CTime::GetCurrentTime();
for(int j = 0; j< 200000; j++)
{
mult(TestArray,TestA,Result);
}
CTime EndTime = CTime::GetCurrentTime();
CTimeSpan SpendTime = EndTime - BeginTime;
printf("花费 %ld 秒",SpendTime.GetTotalSeconds());
}
int _tmain(int argc, _TCHAR* argv[])
{
for(int i = 0; i<=5; i++)
Mytest();
return 0;
}
实现二:matrix与matrix
#include "stdafx.h"
#include "mtl/matrix.h"
#include "mtl/dense1D.h"
#include "mtl/mtl.h"
#include <atltime.h>
using namespace ATL;
using namespace std;
using namespace mtl;
typedef matrix<double, rectangle<>, dense<>,row_major>::type Matrix;
void Mytest()
{
const Matrix::size_type MAX_ROW = 100, MAX_COL = 100, TestA_ROW =1 , TestA_COL =100;
Matrix TestArray(MAX_ROW,MAX_COL),TestA(TestA_ROW,TestA_COL),Result(TestA_ROW,TestA_COL);
for (Matrix::size_type i=0; i<MAX_ROW; ++i)
{
for (Matrix::size_type j=0; j<MAX_COL; ++j)
{
TestArray(i, j) = Matrix::value_type(j);
}
}
for (Matrix::size_type i=0; i<TestA_ROW; ++i)
{
for (Matrix::size_type j=0; j<TestA_COL; ++j)
{
TestA(i, j) = Matrix::value_type(j);
}
}
double OutResult = 0;
CTime BeginTime = CTime::GetCurrentTime();
for(int j = 0; j< 200000; j++)
{
mult(TestA,TestArray,Result);
}
CTime EndTime = CTime::GetCurrentTime();
CTimeSpan SpendTime = EndTime - BeginTime;
printf("花费 %ld 秒",SpendTime.GetTotalSeconds());
}
int _tmain(int argc, _TCHAR* argv[])
{
for(int i = 0; i<=5; i++)
Mytest();
return 0;
}
PS:这两个程序的乘顺序不同,因为目的只是评估速度,不必深究,谢谢。