c++开方效率测试

cpp 复制代码
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>

#include <chrono>
#include <cmath>
#include <functional>

void math_test(std::function<float(float)> _function) {
    auto start = std::chrono::high_resolution_clock::now();
    float sum = 0.0f;
    for (float i = 1.0f; i <= 10000000.0f; ++i) {
        sum += _function(i);
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    std::cout << "总和: " << sum << std::endl;
    std::cout << "              耗时: " << duration << " 微秒" << std::endl;
}

void math_test_and(std::function<float(float&)> _function) {
    auto start = std::chrono::high_resolution_clock::now();
    float sum = 0.0f;
    for (float i = 1.0f; i <= 10000000.0f; ++i) {
        sum += _function(i);
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    std::cout << "总和: " << sum << std::endl;
    std::cout << "              耗时: " << duration << " 微秒" << std::endl;
}

inline float std_sqrt(float number)
{
    return 1.0f / std::sqrt(number);
}

inline float std_sqrt_and(float& number)
{
    return 1.0f / std::sqrt(number);
}

inline float q_sqrt(float number)
{
    long i;
    float x2, y;
    const float threehalfs = 1.5F;

    x2 = number * 0.5F;
    y = number;
    // 通过内存地址转换技巧,将浮点数的位模式直接转换为长整数,实现对浮点位的操作
    i = * (long*) &y;
    // 对数运算推导位操作公式
    i = 0x5f3759df - (i >> 1);
    y = * (float*) &i;
    // 牛顿法对初步近似结果进行一次迭代优化,将误差控制在1%以内
    y = y * (threehalfs - (x2 * y * y));
    return y;
}

inline float q_sqrt_and(float& number)
{
    long i;
    float x2, y;
    const float threehalfs = 1.5F;

    x2 = number * 0.5F;
    y = number;
    i = *(long*)&y;
    i = 0x5f3759df - (i >> 1);
    y = *(float*)&i;
    y = y * (threehalfs - (x2 * y * y));
    return y;
}

float q_sqrt_and_out(float& number)
{
    long i;
    float x2, y;
    const float threehalfs = 1.5F;

    x2 = number * 0.5F;
    y = number;
    i = *(long*)&y;
    i = 0x5f3759df - (i >> 1);
    y = *(float*)&i;
    y = y * (threehalfs - (x2 * y * y));
    return y;
}

inline float q_s_sqrt(float number)
{
    long i;
    float x2, y;
    const float threehalfs = 1.5f;

    x2 = number * 0.5f;
    y = number;
    i = * (long*) &y;
    i = 0x5f3759df - (i >> 1);
    y = * (float*) &i;
    y = y * (threehalfs - (x2 * y * y));
    y = y * (threehalfs - (x2 * y * y));
    return y;
}

inline float q_s_sqrt_and(float& number)
{
    long i;
    float x2, y;
    const float threehalfs = 1.5f;

    x2 = number * 0.5f;
    y = number;
    i = *(long*)&y;
    i = 0x5f3759df - (i >> 1);
    y = *(float*)&i;
    y = y * (threehalfs - (x2 * y * y));
    y = y * (threehalfs - (x2 * y * y));
    return y;
}

int main()
{
    std::cout << "Hello World!\n";

    math_test(std_sqrt);

    math_test(q_sqrt);

    math_test(q_s_sqrt);

    std::cout << "------------------------------------------" << std::endl;

    math_test_and(std_sqrt_and);

    math_test_and(q_sqrt_and);

    math_test_and(q_s_sqrt_and);

    std::cout << "------------------------------------------" << std::endl;

    auto start = std::chrono::high_resolution_clock::now();
    float sum = 0.0f;
    for (float i = 1.0f; i <= 10000000.0f; ++i) {
        sum += std_sqrt(i);
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    std::cout << "总和: " << sum << std::endl;
    std::cout << "              耗时: " << duration << " 微秒" << std::endl;

    start = std::chrono::high_resolution_clock::now();
    sum = 0.0f;
    for (float i = 1.0f; i <= 10000000.0f; ++i) {
        sum += q_s_sqrt(i);
    }
    end = std::chrono::high_resolution_clock::now();
    duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    std::cout << "总和: " << sum << std::endl;
    std::cout << "              耗时: " << duration << " 微秒" << std::endl;

    start = std::chrono::high_resolution_clock::now();
    sum = 0.0f;
    for (float i = 1.0f; i <= 10000000.0f; ++i) {
        sum += q_sqrt(i);
    }
    end = std::chrono::high_resolution_clock::now();
    duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    std::cout << "总和: " << sum << std::endl;
    std::cout << "              耗时: " << duration << " 微秒" << std::endl;

    start = std::chrono::high_resolution_clock::now();
    sum = 0.0f;
    for (float i = 1.0f; i <= 10000000.0f; ++i) {
        sum += q_sqrt_and(i);
    }
    end = std::chrono::high_resolution_clock::now();
    duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    std::cout << "总和: " << sum << std::endl;
    std::cout << "              耗时: " << duration << " 微秒" << std::endl;

    start = std::chrono::high_resolution_clock::now();
    sum = 0.0f;
    for (float i = 1.0f; i <= 10000000.0f; ++i) {
        sum += q_sqrt_and_out(i);
    }
    end = std::chrono::high_resolution_clock::now();
    duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    std::cout << "总和: " << sum << std::endl;
    std::cout << "              耗时: " << duration << " 微秒" << std::endl;

    start = std::chrono::high_resolution_clock::now();
    sum = 0.0f;
    for (float i = 1.0f; i <= 10000000.0f; ++i) {
        long _i;
        float x2, y;
        const float threehalfs = 1.5F;

        x2 = i * 0.5F;
        y = i;
        _i = *(long*)&y;
        _i = 0x5f3759df - (_i >> 1);
        y = *(float*)&_i;
        y = y * (threehalfs - (x2 * y * y));

        sum += y;
    }
    end = std::chrono::high_resolution_clock::now();
    duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    std::cout << "总和: " << sum << std::endl;
    std::cout << "              耗时: " << duration << " 微秒" << std::endl;
}
相关推荐
老四啊laosi8 小时前
[C++进阶] 24. 哈希表封装unordered_map && unordered_set
c++·哈希表·封装·unordered_map·unordered_set
妙为9 小时前
银河麒麟V4下编译Qt5.12.12源码
c++·qt·国产化·osg3.6.5·osgearth3.2·银河麒麟v4
史迪仔011212 小时前
[QML] QML IMage图像处理
开发语言·前端·javascript·c++·qt
会编程的土豆13 小时前
【数据结构与算法】再次全面了解LCS底层
开发语言·数据结构·c++·算法
低频电磁之道13 小时前
解决 Windows C++ DLL 导出类不可见的编译错误
c++·windows
君义_noip15 小时前
信息学奥赛一本通 4150:【GESP2509七级】⾦币收集 | 洛谷 P14078 [GESP202509 七级] 金币收集
c++·算法·gesp·信息学奥赛·csp-s
Ricky_Theseus15 小时前
静态链接与动态链接
c++
澈20715 小时前
双指针,数组去重
c++·算法
小辉同志16 小时前
207. 课程表
c++·算法·力扣·图论
feng_you_ying_li16 小时前
C++11,{}的初始化情况与左右值及其引用
开发语言·数据结构·c++