时钟()returns0
clock() returns 0
当我 运行 下面的代码得到值 0 时,有几次我确实得到了 intAddition 的值。我已经尝试了很多我在网上找到的建议,但还没有成功。我的同学向我展示了他是如何做的,这与我的非常相似。他从他的程序中得到的值很小,从 1 到 3。
感谢您的帮助!
#include <iostream>
#include <time.h>
#include <stdio.h>
clock_t start, end;
void intAddition(int a, int b){
start = clock();
a + b;
end = clock();
printf("CPU cycles to execute integer addition operation: %d\n", end-start);
}
void intMult(int a, int b){
start = clock();
a * b;
end = clock();
printf("CPU cycles to execute integer multiplication operation: %d\n", end-start);
}
void floatAddition(float a, float b){
start = clock();
a + b;
end = clock();
printf("CPU cycles to execute float addition operation: %d\n", end-start);
}
void floatMult(float a, float b){
start = clock();
a * b;
end = clock();
printf("CPU cycles to execute float multiplication operation: %d\n", end-start);
}
int main()
{
int a,b;
float c,d;
a = 3, b = 6;
c = 3.7534, d = 6.85464;
intAddition(a,b);
intMult(a,b);
floatAddition(c,d);
floatMult(c,d);
return 0;
}
在某些编译器上,clock() 以毫秒为单位测量时间。此外,编译器对于简单测试来说太聪明了,它可能会跳过所有内容,因为这些操作的结果没有被使用。
例如,此循环可能需要不到 1 毫秒(除非调试器打开或优化关闭)
int R = 1;
int a = 2;
int b = 3;
start = clock();
for( int i = 0; i < 10000000; i++ )
R = a * b;
printf( "time passed: %ld\n", clock() - start );
R 始终是相同的数字 (6),甚至没有使用 R。编译器可能会跳过所有计算。你必须在最后打印R或者做一些其他事情来欺骗编译器配合测试。
由 clock()
编辑的值 return 属于 clock_t
类型(实现定义的算术类型)。它代表“实现对处理器的最佳近似
自与实现定义时代相关的开始以来程序使用的时间
仅对程序调用”(N1570 7.27.2.1)。
给定一个 clock_t
值,您可以通过将它乘以 CLOCKS_PER_SEC
来确定它代表的秒数,<time.h>
中定义的实现定义的宏。 POSIX 要求 CLOCKS_PER_SEC
为一百万,但在不同的系统上可能有不同的值。
请特别注意,CLOCKS_PER_SEC
的值 而不是 必须对应于 clock()
函数的实际精度。
根据实施情况,如果 CPU 消耗的时间小于 clock()
的精度,则对 clock()
的两次连续调用可能 return 相同的值] 功能。在我测试的一个系统上,clock()
函数的分辨率是 0.01 秒; CPU 可以在那段时间内执行 很多 条指令。
这是一个测试程序:
#include <stdio.h>
#include <time.h>
#include <limits.h>
int main(void) {
long count = 0;
clock_t c0 = clock(), c1;
while ((c1 = clock()) == c0) {
count ++;
}
printf("c0 = %ld, c1 = %ld, count = %ld\n",
(long)c0, (long)c1, count);
printf("clock_t is a %d-bit ", (int)sizeof (clock_t) * CHAR_BIT);
if ((clock_t)-1 > (clock_t)0) {
puts("unsigned integer type");
}
else if ((clock_t)1 / 2 == 0) {
puts("signed integer type");
}
else {
puts("floating-point type");
}
printf("CLOCKS_PER_SEC = %ld\n", (long)CLOCKS_PER_SEC);
return 0;
}
在一个系统 (Linux x86_64) 上,输出是:
c0 = 831, c1 = 833, count = 0
clock_t is a 64-bit signed integer type
CLOCKS_PER_SEC = 1000000
显然在该系统上,clock()
函数的实际分辨率是一或两微秒,并且连续两次调用 clock()
return 不同的值。
在另一个系统 (Solaris SPARC) 上,输出为:
c0 = 0, c1 = 10000, count = 10447
clock_t is a 32-bit signed integer type
CLOCKS_PER_SEC = 1000000
在该系统上,clock()
函数的分辨率为 0.01 秒(10,000 微秒),return 由 clock()
编辑的值在数千次迭代中没有改变。
还有(至少)一件事需要注意。在 clock_t
是 32 位的系统上,使用 CLOCKS_PER_SEC == 1000000
,该值可以在大约 72 分钟的 CPU 时间后回绕,这对于长 运行 程序可能很重要.有关详细信息,请参阅您的系统文档。
当我 运行 下面的代码得到值 0 时,有几次我确实得到了 intAddition 的值。我已经尝试了很多我在网上找到的建议,但还没有成功。我的同学向我展示了他是如何做的,这与我的非常相似。他从他的程序中得到的值很小,从 1 到 3。
感谢您的帮助!
#include <iostream>
#include <time.h>
#include <stdio.h>
clock_t start, end;
void intAddition(int a, int b){
start = clock();
a + b;
end = clock();
printf("CPU cycles to execute integer addition operation: %d\n", end-start);
}
void intMult(int a, int b){
start = clock();
a * b;
end = clock();
printf("CPU cycles to execute integer multiplication operation: %d\n", end-start);
}
void floatAddition(float a, float b){
start = clock();
a + b;
end = clock();
printf("CPU cycles to execute float addition operation: %d\n", end-start);
}
void floatMult(float a, float b){
start = clock();
a * b;
end = clock();
printf("CPU cycles to execute float multiplication operation: %d\n", end-start);
}
int main()
{
int a,b;
float c,d;
a = 3, b = 6;
c = 3.7534, d = 6.85464;
intAddition(a,b);
intMult(a,b);
floatAddition(c,d);
floatMult(c,d);
return 0;
}
在某些编译器上,clock() 以毫秒为单位测量时间。此外,编译器对于简单测试来说太聪明了,它可能会跳过所有内容,因为这些操作的结果没有被使用。
例如,此循环可能需要不到 1 毫秒(除非调试器打开或优化关闭)
int R = 1;
int a = 2;
int b = 3;
start = clock();
for( int i = 0; i < 10000000; i++ )
R = a * b;
printf( "time passed: %ld\n", clock() - start );
R 始终是相同的数字 (6),甚至没有使用 R。编译器可能会跳过所有计算。你必须在最后打印R或者做一些其他事情来欺骗编译器配合测试。
由 clock()
编辑的值 return 属于 clock_t
类型(实现定义的算术类型)。它代表“实现对处理器的最佳近似
自与实现定义时代相关的开始以来程序使用的时间
仅对程序调用”(N1570 7.27.2.1)。
给定一个 clock_t
值,您可以通过将它乘以 CLOCKS_PER_SEC
来确定它代表的秒数,<time.h>
中定义的实现定义的宏。 POSIX 要求 CLOCKS_PER_SEC
为一百万,但在不同的系统上可能有不同的值。
请特别注意,CLOCKS_PER_SEC
的值 而不是 必须对应于 clock()
函数的实际精度。
根据实施情况,如果 CPU 消耗的时间小于 clock()
的精度,则对 clock()
的两次连续调用可能 return 相同的值] 功能。在我测试的一个系统上,clock()
函数的分辨率是 0.01 秒; CPU 可以在那段时间内执行 很多 条指令。
这是一个测试程序:
#include <stdio.h>
#include <time.h>
#include <limits.h>
int main(void) {
long count = 0;
clock_t c0 = clock(), c1;
while ((c1 = clock()) == c0) {
count ++;
}
printf("c0 = %ld, c1 = %ld, count = %ld\n",
(long)c0, (long)c1, count);
printf("clock_t is a %d-bit ", (int)sizeof (clock_t) * CHAR_BIT);
if ((clock_t)-1 > (clock_t)0) {
puts("unsigned integer type");
}
else if ((clock_t)1 / 2 == 0) {
puts("signed integer type");
}
else {
puts("floating-point type");
}
printf("CLOCKS_PER_SEC = %ld\n", (long)CLOCKS_PER_SEC);
return 0;
}
在一个系统 (Linux x86_64) 上,输出是:
c0 = 831, c1 = 833, count = 0
clock_t is a 64-bit signed integer type
CLOCKS_PER_SEC = 1000000
显然在该系统上,clock()
函数的实际分辨率是一或两微秒,并且连续两次调用 clock()
return 不同的值。
在另一个系统 (Solaris SPARC) 上,输出为:
c0 = 0, c1 = 10000, count = 10447
clock_t is a 32-bit signed integer type
CLOCKS_PER_SEC = 1000000
在该系统上,clock()
函数的分辨率为 0.01 秒(10,000 微秒),return 由 clock()
编辑的值在数千次迭代中没有改变。
还有(至少)一件事需要注意。在 clock_t
是 32 位的系统上,使用 CLOCKS_PER_SEC == 1000000
,该值可以在大约 72 分钟的 CPU 时间后回绕,这对于长 运行 程序可能很重要.有关详细信息,请参阅您的系统文档。