小编jpm*_*orr的帖子

快速 C++ 符号函数

在我的代码中,我在循环中多次对 double 进行符号检查,并且该循环通常在执行期间运行数百万次。

我的符号检查是一个非常基本的计算,fabs()所以我认为必须有其他方法可以更快,因为“分割很慢”。我遇到了一个模板函数,copysign()并创建了一个简单的程序来运行速度比较。我已经用下面的代码测试了三种可能的解决方案。

// C++ program to find out execution time of  of functions 
#include <chrono> 
#include <iostream> 
#include <math.h>

using namespace std; 
using namespace std::chrono; 

template<typename Clock>

void printResult(const std::string name, std::chrono::time_point<Clock> start, std::chrono::time_point<Clock> stop, const int iterations)
{
    // Get duration. 
    std::chrono::duration my_duration = duration_cast<nanoseconds>(stop - start); 
    my_duration /= iterations;

    cout << "Time taken by "<< name <<" function: " << my_duration.count() << " ns avg. for " << iterations << " iterations." …
Run Code Online (Sandbox Code Playgroud)

c++ benchmarking timing

0
推荐指数
1
解决办法
106
查看次数

标签 统计

benchmarking ×1

c++ ×1

timing ×1