通过测量一次迭代来估计C ++函数的总运行时间

问题描述

我已经实现了一种c ++方法,该方法可以在给定的时间间隔内计算近似值和参考函数间的最大ulp误差。近似值和参考值均作为单精度浮点值进行计算。该方法从间隔的下限开始,并迭代范围内的每个现有单精度值。

由于根据选择的范围存在很多现有值,因此我想估算此方法的总运行时间,并将其打印给用户

我试图多次执行比较,以计算一次迭代的运行时间。我的方法是将一次迭代的持续时间乘以该范围内存在的浮点总数。但是很明显,一次迭代的执行时间不是恒定的,而是取决于迭代次数,因此我估算的持续时间根本不准确...也许有人可以在主循环中调整总的运行时计算?

我的问题是:对于这种特殊情况,还有其他方法可以估算总运行时间吗?

这是我的代码

void FloatEvaluateMaxUlp(float(*testFunction)(float),float(*referenceFunction)(float),float lowBound,float highBound)
{
    /*initialization*/
    float x = lowBound,output,output_ref;
    int ulp = 0;
    long long duration = 0,numberOfFloats=0;

    /*calculate number of floats between lowBound and highBound*/
    numberOfFloats = *(int*)&highBound - *(int*)&lowBound;

    /*measure execution time of 10 iterations*/
    int iterationsToEstimateTime = 1000;
    auto t1 = std::chrono::high_resolution_clock::Now();
    for (int i = 0; i < iterationsToEstimateTime; i++)
    {
        printProgressInteger(i+1,iterationsToEstimateTime);
        output = testFunction(x);
        output_ref = referenceFunction(x);
        int ulp_local = FloatCompareULP(output,output_ref);
        if (abs(ulp_local) > abs(ulp))
            ulp = ulp_local;
        x= std::nextafter(x,highBound + 0.001f);
    }
    auto t2 = std::chrono::high_resolution_clock::Now();
    duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
    duration /= iterationsToEstimateTime;
    x = lowBound;
    


    /*output of estimated time*/
    std::cout <<std::endl<<std::endl<< " Number of floats: " << numberOfFloats << "   Time per iteration: " << duration << "  Estimated total time: " << numberOfFloats * duration << std::endl;
    std::cout << " Starting test in range [" << lowBound << "," << highBound << "]." << std::endl;


    long long count = 0;

    /*record start time*/
    t1 = std::chrono::high_resolution_clock::Now();

    
    for (count; x < highBound; count++)
    {
        printProgressInteger(count,numberOfFloats);
        output = testFunction(x);
        output_ref = referenceFunction(x);
        int ulp_local = FloatCompareULP(output,output_ref);
        if (abs(ulp_local) > abs(ulp))
            ulp = ulp_local;
        x = std::nextafter(x,highBound + 0.001f);

    }

    /*record stop time and compute duration*/
    t2 = std::chrono::high_resolution_clock::Now();
    duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();

    /*result output*/
    std::cout <<std::endl<< std::endl << std::endl << std::endl << "*********************************************************" << std::endl;
    std::cout << "                   RESULT                                " << std::endl;
    std::cout << "*********************************************************" << std::endl;
    std::cout << " Iterations: " << count << "  Total execution time: " << duration << std::endl;
    std::cout << " Max ulp: " << ulp <<std::endl;
    std::cout << "*********************************************************" << std::endl;


}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...