Iomanip set precision() 方法不能正常工作,只在第一行,为什么?
因此,我正在编写一个程序来使用时钟来计算函数的执行时间,并使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确地看下面这行:
看到了吗?您正在打印
time_taken
后设置精度。因此,您第一次看不到set precision()
的结果。但第二次及以后,由于set precision()
已被执行,您将获得所需的小数位数。因此,要解决此问题,请将
set precision()
移至time_taken
之前,如下所示:..或者您也可以执行类似以下操作:
此外,请考虑不在您的代码中使用以下行 :代码:
..因为它被认为是一种不好的做法。相反,每次都使用 std:: 像这样:
有关更多信息,请查找 为什么“使用命名空间 std”被认为是一种不好的实践。
Look at the following line properly:
See? You are setting the precision after printing out
time_taken
. So for the first time, you don't see the result ofsetprecision()
. But for the second time and onwards, assetprecision()
has already been executed, you get the desired decimal places.So to fix this issue, move
setprecision()
beforetime_taken
as such:..or you can also do something like this:
Also, consider not using the following line in your code:
..as it's considered as a bad practice. Instead use std:: every time like this:
For more information on this, look up to why is "using namespace std" considered as a bad practice.