Iomanip set precision() 方法不能正常工作,只在第一行,为什么?

发布于 2025-01-10 19:23:11 字数 1400 浏览 0 评论 0原文

因此,我正在编写一个程序来使用时钟来计算函数的执行时间,并使用 iomanip 将输出更改为带有 9 个零的十进制。

这是我正在使用的代码:

#include <time.h>
#include <iomanip>

using namespace std;

void linearFunction(int input)
{
    for(int i = 0; i < input; i++)
    {

    }
}

void execution_time(int input)
{
    clock_t start_time, end_time;

    start_time = clock();
    linearFunction(input);
    end_time = clock();

    double time_taken = double(end_time - start_time) / double(CLOCKS_PER_SEC);
    cout << "Time taken by function for input = " << input << " is : " << fixed
         << time_taken << setprecision(9);
    cout << " sec " << endl;
}

int main()
{
    execution_time(10000);
    execution_time(100000);
    execution_time(1000000);
    execution_time(10000000);
    execution_time(100000000);
    execution_time(1000000000);

    return 0;
}

输出显示:

Time taken by function for input = 10000 is : 0.000000 sec
Time taken by function for input = 100000 is : 0.001000000 sec
Time taken by function for input = 1000000 is : 0.002000000 sec
Time taken by function for input = 10000000 is : 0.038000000 sec
Time taken by function for input = 100000000 is : 0.316000000 sec
Time taken by function for input = 1000000000 is : 3.288000000 sec

如您所见,我第一次调用该函数时,它不遵循我编写的 set precision(9) 。这是为什么?我该如何解决这个问题?预先感谢您。

So I'm writing a program to count the execution time of a function using clock and I used iomanip to change the output to decimal with 9 zeros.

This is the code that I am using:

#include <time.h>
#include <iomanip>

using namespace std;

void linearFunction(int input)
{
    for(int i = 0; i < input; i++)
    {

    }
}

void execution_time(int input)
{
    clock_t start_time, end_time;

    start_time = clock();
    linearFunction(input);
    end_time = clock();

    double time_taken = double(end_time - start_time) / double(CLOCKS_PER_SEC);
    cout << "Time taken by function for input = " << input << " is : " << fixed
         << time_taken << setprecision(9);
    cout << " sec " << endl;
}

int main()
{
    execution_time(10000);
    execution_time(100000);
    execution_time(1000000);
    execution_time(10000000);
    execution_time(100000000);
    execution_time(1000000000);

    return 0;
}

And the output shows:

Time taken by function for input = 10000 is : 0.000000 sec
Time taken by function for input = 100000 is : 0.001000000 sec
Time taken by function for input = 1000000 is : 0.002000000 sec
Time taken by function for input = 10000000 is : 0.038000000 sec
Time taken by function for input = 100000000 is : 0.316000000 sec
Time taken by function for input = 1000000000 is : 3.288000000 sec

As you can see, the first time I call the function, it doesn't follow the setprecision(9) that I wrote. Why is this and how can I solve this? Thanks you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼中杀气 2025-01-17 19:23:11

正确地看下面这行:

cout << "Time taken by function for input = " << input << " is : " << fixed << time_taken << setprecision(9);

看到了吗?您正在打印 time_taken 后设置精度。因此,您第一次看不到 set precision() 的结果。但第二次及以后,由于 set precision() 已被执行,您将获得所需的小数位数。

因此,要解决此问题,请将 set precision() 移至 time_taken 之前,如下所示:

cout << "Time taken by function for input = " << input << " is : " << fixed << setprecision(9) << time_taken;

..或者您也可以执行类似以下操作:

cout.precision(9);
cout << "Time taken by function for input = " << input << " is : " << fixed << time_taken;

此外,请考虑不在您的代码中使用以下行 :代码:

using namespace std;

..因为它被认为是一种不好的做法。相反,每次都使用 std:: 像这样:

std::cout.precision(9);
std::cout << "Time taken by function for input = " << input << " is : " << std::fixed << time_taken;

有关更多信息,请查找 为什么“使用命名空间 std”被认为是一种不好的实践

Look at the following line properly:

cout << "Time taken by function for input = " << input << " is : " << fixed << time_taken << setprecision(9);

See? You are setting the precision after printing out time_taken. So for the first time, you don't see the result of setprecision(). But for the second time and onwards, as setprecision() has already been executed, you get the desired decimal places.

So to fix this issue, move setprecision() before time_taken as such:

cout << "Time taken by function for input = " << input << " is : " << fixed << setprecision(9) << time_taken;

..or you can also do something like this:

cout.precision(9);
cout << "Time taken by function for input = " << input << " is : " << fixed << time_taken;

Also, consider not using the following line in your code:

using namespace std;

..as it's considered as a bad practice. Instead use std:: every time like this:

std::cout.precision(9);
std::cout << "Time taken by function for input = " << input << " is : " << std::fixed << time_taken;

For more information on this, look up to why is "using namespace std" considered as a bad practice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文