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;
}
相关推荐
SmartRadio10 小时前
ESP32添加修改蓝牙名称和获取蓝牙连接状态的AT命令-完整UART BLE服务功能后的完整`main.c`代码
c语言·开发语言·c++·esp32·ble
charlie11451419111 小时前
嵌入式的现代C++教程——constexpr与设计技巧
开发语言·c++·笔记·单片机·学习·算法·嵌入式
CSDN_RTKLIB14 小时前
【字符编码】有无BOM的UTF-8
c++
Chary201615 小时前
opengl 学习资料路径
c++·opengl
im_AMBER15 小时前
Leetcode 102 反转链表
数据结构·c++·学习·算法·leetcode·链表
今儿敲了吗15 小时前
01|多项式输出
c++·笔记·算法
程序员Jared15 小时前
C++11—mutex
c++
朔北之忘 Clancy16 小时前
2025 年 9 月青少年软编等考 C 语言一级真题解析
c语言·开发语言·c++·学习·数学·青少年编程·题解
量子炒饭大师16 小时前
【C++入门】Cyber底码作用域的隔离协议——【C++命名空间】(using namespace std的原理)
开发语言·c++·dubbo
REDcker16 小时前
RTCP 刀尖点跟随技术详解
c++·机器人·操作系统·嵌入式·c·数控·机床