使用clock() 获取时间戳(Visual Studio 2010,C/C++)
好的,所以我尝试用 C 实现 Collatz 问题,并记录/打印 while 循环执行所需的时间。我应该报告“滴答声”的数量和以秒为单位的时间。然而,我的代码中出现了一些看似简单的错误,但无论出于何种原因,我不知道如何纠正它们。
这是我的代码
#include <stdio.h>
#include <time.h>
void main() {
int n, c = 0;
printf("Please enter an integer...\n");
scanf("%d", &n);
clock_t start; /* Line 8 */
clock_t finish; /* Line 9 */
start = clock();
while (n != 1) {
if (n%2 == 0)
n = n/2;
else
n = (3*n)+1;
c++;
printf("n=%d\n", n);
}
finish = clock() - start;
double interval = finish / (double)CLOCKS_PER_SEC;
printf("%d iterations\n", c);
printf("%f clock cycles", finish);
printf("%f seconds elapsed", interval);
}
这些是 Visual Studio 报告的错误
第 8 行和第 9 行错误
“clock_t”:非法使用此类型作为表达式
语法错误:缺少“;”在标识符“开始”
“开始”之前:未声明的标识符
对于出现“开始”或“完成”的所有行,我也收到“未声明的标识符”错误
Ok, so I am trying to implement the Collatz problem in C, and record/print the time it takes for the while loop to execute. I am supposed to report both the number of "ticks" and the time in seconds. However, I am getting some seemingly simple errors from my code but for whatever reason, I am not sure how to correct them.
This is my code
#include <stdio.h>
#include <time.h>
void main() {
int n, c = 0;
printf("Please enter an integer...\n");
scanf("%d", &n);
clock_t start; /* Line 8 */
clock_t finish; /* Line 9 */
start = clock();
while (n != 1) {
if (n%2 == 0)
n = n/2;
else
n = (3*n)+1;
c++;
printf("n=%d\n", n);
}
finish = clock() - start;
double interval = finish / (double)CLOCKS_PER_SEC;
printf("%d iterations\n", c);
printf("%f clock cycles", finish);
printf("%f seconds elapsed", interval);
}
These are the errors Visual Studio is reporting
Line 8 and 9 Errors
'clock_t' : illegal use of this type as an expression
syntax error : missing ';' before identifier 'start'
'start' : undeclared identifier
I am also getting an 'undeclared identifier' error for all lines in which 'start' or 'finish' show up
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Microsoft Visual C++ 不支持 C99,但旧的 C 标准必须在以下位置定义变量每个块的顶部。因此,将您的代码更改为:
Microsoft Visual C++ doesn't support C99, but older C standards where variables have to be defined at the top of each block. So change your code to:
尝试将变量移到任何语句之前的顶部...
其余的错误只是这些未正确声明的结果。
在后来的 C 标准 (C99) 中,声明可以混合在任何地方。通常,使其兼容会更容易...另一种方法是引入一个块 { } ,如下所示:
在这种情况下,它不是一个理想的解决方案,因为它足够简单,可以重新排列代码。但有时,当尝试在 C89 中编译 C99 代码而不需要过多重新安排内容时,这很有用。
Try moving the variables to the top before any statements...
The rest of your errors are just consequences of these not being declared properly.
In a later C standard (C99) declarations can be mixed in anywhere. Often it's just easier to make it compliant... Another approach is to introduce a block { } like so:
In this case, it's not an ideal solution as it's simple enough to rearrange your code. But sometimes this is useful when trying to make C99 code compile in C89 without rearranging things too much.
如果您使用 Microsoft 的编译器(与 Visual Studio 捆绑在一起的编译器)将代码编译为 C,那么您需要注意它实际上并不支持 C99 标准。你被 C89 困住了。
C89 中缺少的最令人恼火的事情之一是在任何地方声明变量的能力。相反,您被迫在块的顶部声明它们。
当您未能遵循此规则时,出现的编译器错误通常相当难以描述。我经常犯这个错误,并花了几秒钟的时间对输出感到困惑。
因此,将代码更改为如下所示,在功能块的 top 处声明
start
和finish
:另请注意,没有这样的东西如 C 中的
void main()
。main
函数的函数原型始终是以下之一:If you're compiling code as C using Microsoft's compiler (the one bundled with Visual Studio), then you need to be aware that it does not actually support the C99 standard. You're stuck with C89.
And one of the most infuriating things that is missing in C89 is the ability to declare variables anywhere. Instead, you're forced to declare them at the top of a block.
The compiler errors that you get when you fail to follow this rule are often rather undescriptive. I make this mistake quite frequently and spend a few seconds puzzling at the output.
So change your code to look like this, declaring
start
andfinish
at the top of the function block:Also note that there is no such thing as
void main()
in C. The function prototype of themain
function is always one of the following: