gpt4 book ai didi

performance - c++,cin 有问题吗?

转载 作者:行者123 更新时间:2023-11-28 21:15:54 24 4
gpt4 key购买 nike

<分区>

我写了一个小测试来找出对特殊 x 最快的数学运算。我想让用户输入 x,这样我就可以针对不同的 x 运行测试。在下面的代码中,我告诉我 std::cin >> val; 有错误错误:无法将“std::istream {aka std::basic_istream}”左值绑定(bind)到“std::basic_istream&&”

如果我将 val 声明为 double val 而不是 const double val 我会得到更多错误。我可以更改什么以使程序运行?

#include <cmath>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <istream>
#include <ostream>
// for x^1.5
double test_pow_15(double x) { return std::pow(x, 1.5); };
double test_chain_15(double x) { return sqrt(x * x * x); };
double test_tmp_15(double x) { double tmp = x * x * x; return sqrt(tmp); };


volatile double sink;
const double val = 0;
const double ans_15 = std::pow(val, 1.5);

void do_test(const char* name, double(&fn)(double), const double ans) {
auto start = std::chrono::high_resolution_clock::now();
for (size_t n = 0; n < 1000 * 1000 * 10; ++n) {
sink = val;
sink = fn(sink);
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> dur = end - start;
std::cout << name << ".Took" << dur.count() << "ms, error:" << sink - ans << '\n';
}

int main()
{
std::cout << "Speed test"<< '\n';
std::cout << "Please enter value for x."<< '\n';
std::cout << "x = ";
std::cin >> val;

std::cout << "Speed test starts for x = "<< val <<"."<<'\n';
std::cout << " " << '\n';
std::cout << "For " << val<<"^(1.5) the speed is:" <<'\n';
do_test("std::pow(x,1.5) ",test_pow_15, ans_15);
do_test("sqrt(x*x*x) ",test_chain_15, ans_15);
do_test("tmp = x*x*x; sqrt(tmp) ",test_tmp_15, ans_15);

return 0;
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com