testmath.cpp

来自「ncbi源码」· C++ 代码 · 共 625 行 · 第 1/2 页

CPP
625
字号
    /// FIXME: implement determinants...    //float det = mat4.determinant();    //cout << "matrix is " << endl;    //cout << mat4;    //cout << "determinant is " << det << endl;}////// test the speed of matrix addition in the old and new matrix schemesvoid testMatrixAddTime(size_t iters){    cout << "testing matrix addition time..." << endl;    CStopWatch sw;    {{         cout << "CMatrix3<>: ";         cout.flush();         CMatrix3<float> mat (1.0f, 1.0f, 1.0f,                              1.0f, 1.0f, 1.0f,                              1.0f, 1.0f, 1.0f);         sw.Start();         for (size_t i = 0;  i < iters;  ++i) {             for (size_t j = 0;  j < iters;  ++j) {                 mat += mat;             }         }         cout << "elapsed time: " << sw.Elapsed() << endl;     }}    {{         for (size_t r = 3;  r <= 10;  ++r) {             cout << "CNcbiMatrix<>(" << r << ", " << r << "): ";             cout.flush();             CNcbiMatrix<float> mat (r, r, 1.0f);             sw.Start();             for (size_t i = 0;  i < iters;  ++i) {                 for (size_t j = 0;  j < iters;  ++j) {                     mat += mat;                 }             }             cout << "elapsed time: " << sw.Elapsed() << endl;         }     }}}////// test the speed of vector addition in the old and new vector schemesvoid testVectorAddTime(size_t iters){    cout << "testing vector addition time..." << endl;    CVect3<float> old_vect3 (1.0f, 1.0f, 1.0f);    CStopWatch sw;    sw.Start();    for (size_t i = 0;  i < iters;  ++i) {        for (size_t j = 0;  j < iters;  ++j) {            old_vect3 += old_vect3;        }    }    cout << "CVect3<>: elapsed time: " << sw.Elapsed() << endl;#if 0    std::vector<float> new_vect3 (3, 1.0f);    sw.Start();    for (size_t i = 0;  i < iters;  ++i) {        for (size_t j = 0;  j < iters;  ++j) {            new_vect3 += new_vect3;        }    }    cout << "new vector3:" << endl;    cout << "elapsed time: " << sw.Elapsed() << endl;#endif}////// test the speed of vector addition in the old and new vector schemesvoid testVectorDotTime(size_t iters){    cout << "testing vector dot time..." << endl;    float random = rand();    CVect3<float> old_vect3 (random, random, random);    float temp;    CStopWatch sw;    temp = 0;    sw.Start();    for (size_t i = 0;  i < iters;  ++i) {        for (size_t j = 0;  j < iters;  ++j) {            temp += old_vect3.Dot(old_vect3);        }    }    cout << "CVect3<>: elapsed time: " << sw.Elapsed() << endl;    temp = 0;}////// test the speed of matrix multiplication in the old and new matrix schemesvoid testMatrixMultTime(size_t iters){    cout << "testing matrix multiplication time..." << endl;    CStopWatch sw;    {{         cout << "CMatrix4<>: ";         cout.flush();         CMatrix4<float> old_mat4 (1.0f, 1.0f, 1.0f, 1.0f,                                   1.0f, 1.0f, 1.0f, 1.0f,                                   1.0f, 1.0f, 1.0f, 1.0f,                                   1.0f, 1.0f, 1.0f, 1.0f);         sw.Start();         for (size_t i = 0;  i < iters;  ++i) {             for (size_t j = 0;  j < iters;  ++j) {                 old_mat4 *= old_mat4;             }         }         cout << "elapsed time: " << sw.Elapsed() << endl;     }}    {{         for (size_t rc = 2;  rc <= 10;  ++rc) {             cout << "CNcbiMatrix<>(" << rc << ", " << rc << "): ";             cout.flush();             CNcbiMatrix<float> mat(rc, rc, 1.0f);             sw.Start();             for (size_t i = 0;  i < iters;  ++i) {                 for (size_t j = 0;  j < iters;  ++j) {                     mat *= mat;                 }             }             cout << "elapsed time: " << sw.Elapsed() << endl;         }     }}}////// test the speed of matrix multiplication in the old and new matrix schemesvoid testMatrixMultVectorTime(size_t iters){    cout << "testing matrix * vector multiplication time..." << endl;    CStopWatch sw;    {{         CMatrix3<float> old_mat3 (1.0f, 1.0f, 1.0f,                                   1.0f, 1.0f, 1.0f,                                   1.0f, 1.0f, 1.0f);         CVect3<float> old_vec3 (1.0f, 1.0f, 1.0f);         cout << "CMatrx3<> * CVect3<>: ";         cout.flush();         sw.Start();         for (size_t i = 0;  i < iters;  ++i) {             for (size_t j = 0;  j < iters;  ++j) {                 old_vec3 = old_vec3 * old_mat3;             };         }         cout << "elapsed time: " << sw.Elapsed() << endl;     }}    {{         for (size_t rc = 2;  rc <= 10;  ++rc) {             std::vector<float> vec (rc, 1.0f);             CNcbiMatrix<float> mat (rc, rc, 1.0f);             cout << "CNcbiMatrix<>(" << rc << ", " << rc                 << ") * vector<>(" << rc << "): ";             cout.flush();             sw.Start();             for (size_t i = 0;  i < iters;  ++i) {                 for (size_t j = 0;  j < iters;  ++j) {                     vec = vec * mat;                 };             }             cout << "elapsed time: " << sw.Elapsed() << endl;         }     }}}////// test promotion rulesvoidtestPromotion(){    cout << "sizeof(Promote(char, int)): "        << sizeof(SPromoteTraits<char, int>::TPromote) << endl;    cout << "sizeof(Promote(char, float)): "        << sizeof(SPromoteTraits<char, float>::TPromote) << endl;    cout << "sizeof(Promote(float, CVect3<float>)): "        << sizeof(SPromoteTraits<float, CVect3<float> >::TPromote) << endl;    CVect3<float> float_vec(1.1f, 1.1f, 1.1f);    CVect3<int> int_vec(2, 3, 4);    CVect3<float> res;    res = int_vec + float_vec;    cout << "float vec: "        << float_vec[0] << ", " << float_vec[1] << ", " << float_vec[2] << endl;    cout << "int vec: "        << int_vec[0] << ", " << int_vec[1] << ", " << int_vec[2] << endl;    res = float_vec + int_vec;    cout << "f + i = "        << res[0] << ", " << res[1] << ", " << res[2] << endl;    res = int_vec + float_vec;    cout << "i + f = "        << res[0] << ", " << res[1] << ", " << res[2] << endl;}///////////////////////////////////////////////////////////////////////////////  Run test(printout arguments obtained from command - line)int CTestmathApp::Run(void){    // Get arguments    CArgs args = GetArgs();    size_t iters = args["iters"].AsInteger();    testVectorAdd();    testVectorSubtract();    testMatrixAdd();    testMatrixSubtract();    testMatrixMult();    testVectorAddTime(iters);    testVectorDotTime(iters);    testMatrixAddTime(iters);    testMatrixMultVectorTime(iters);    testMatrixMultTime(iters);    testDeterminant();    //testGaussJordan();    //testGaussJordanTime();    testPromotion();    return 0;}///////////////////////////////////////////////////////////////////////////////  Cleanupvoid CTestmathApp::Exit(void){    SetDiagStream(0);}///////////////////////////////////////////////////////////////////////////////  MAINint main(int argc, const char* argv[]){    // Execute main application function    return CTestmathApp().AppMain(argc, argv, 0, eDS_Default, 0);}/* * =========================================================================== * $Log: testmath.cpp,v $ * Revision 1000.2  2004/06/01 20:50:11  gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.5 * * Revision 1.5  2004/05/21 22:27:43  gorelenk * Added PCH ncbi_pch.hpp * * Revision 1.4  2004/04/07 12:51:14  dicuccio * Removed unused variable * * Revision 1.3  2004/02/09 17:37:14  dicuccio * Updated location of matrix class * * Revision 1.2  2004/01/23 13:57:07  dicuccio * Revamped math test app * * Revision 1.1  2003/06/09 19:19:20  dicuccio * Initial revision.  Test app disabled by default. * * =========================================================================== */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?