我想知道将哪些 C 代码行添加到程序中,以便它告诉我程序运行所需的总时间。我猜应该在 main 开始附近和 main 函数结束后有一个计数器初始化。是正确的标题clock.h
?
非常感谢...
更新我有一台 Win Xp 机器。它只是clock()
在程序的开头添加,而clock()
在程序的末尾添加另一个?然后我可以估计时间差。是的,你是对的time.h
。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <share.h>
#include <time.h>
void f(long double fb[], long double fA, long double fB);
int main() {
clock_t start, end;
start = clock();
const int ARRAY_SIZE = 11;
long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);
int i;
long double A, B;
if (z == NULL) {
printf("Out of memory\n");
exit(-1);
}
A = 0.5;
B = 2;
for (i = 0; i < ARRAY_SIZE; i++) {
z[i] = 0;
}
z[1] = 5;
f(z, A, B);
for (i = 0; i < ARRAY_SIZE; i++)
printf("z is %.16Le\n", z[i]);
free(z);
z = NULL;
end = clock();
printf("Took %ld ticks\n", end-start);
printf("Took %f seconds\n", (double)(end-start)/CLOCKS_PER_SEC);
return 0;
}
void f(long double fb[], long double fA, long double fB) {
fb[0] = fb[1]* fA;
fb[1] = fb[1] - 1;
return;
}
MVS2008 的一些错误:
testim.c(16):错误 C2143:语法错误:缺少“;” 在“常量”之前 testim.c(18) :error C2143: 语法错误: 缺少';' 在“类型”之前 testim.c(20) :error C2143: 语法错误: 缺少';' 在“类型”之前 testim.c(21) :error C2143: 语法错误: 缺少';' 在“类型”之前 testim.c(23) :error C2065: 'z' : undeclared identifier testim.c(23) :warning C4047: '==' : 'int' 与 'void *' 的间接级别不同 testim.c(28):错误 C2065:'A':未声明的标识符 testim.c(28) : 警告 C4244: '=' : 从 'double' 转换为 'int',可能丢失数据
它出现28个错误。请注意,如果没有您的时钟代码,我没有任何错误/警告。
最新消息:很遗憾,我在这里没有得到好的答复。但在谷歌上搜索后,代码是有效的。这里是:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
void f(long double fb[], long double fA);
int main() {
clock_t start = clock();
const int ARRAY_SIZE = 11;
long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);
int i;
long double A;
if (z == NULL) {
printf("Out of memory\n");
exit(-1);
}
A = 0.5;
for (i = 0; i < ARRAY_SIZE; i++) {
z[i] = 0;
}
z[1] = 5;
f(z, A);
for (i = 0; i < ARRAY_SIZE; i++)
printf("z is %.16Le\n", z[i]);
free(z);
z = NULL;
printf("Took %f seconds\n", ((double)clock()-start)/CLOCKS_PER_SEC);
return 0;
}
void f(long double fb[], long double fA) {
fb[0] = fb[1]* fA;
fb[1] = fb[1] - 1;
return;
}
干杯
4 月 10 日更新:感谢“JustJeff”,这是一个更好的解决方案
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
void f(long double fb[], long double fA);
const int ARRAY_SIZE = 11;
int main(void)
{
long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);
int i;
long double A;
LARGE_INTEGER freq;
LARGE_INTEGER t0, tF, tDiff;
double elapsedTime;
double resolution;
if (z == NULL) {
printf("Out of memory\n");
exit(-1);
}
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&t0);
// code to be timed goes HERE
{
A = 0.5;
for (i = 0; i < ARRAY_SIZE; i++) {
z[i] = 0;
}
z[1] = 5;
f(z, A);
for (i = 0; i < ARRAY_SIZE; i++)
printf("z is %.16Le\n", z[i]);
free(z);
z = NULL;
}
QueryPerformanceCounter(&tF);
tDiff.QuadPart = tF.QuadPart - t0.QuadPart;
elapsedTime = tDiff.QuadPart / (double) freq.QuadPart;
resolution = 1.0 / (double) freq.QuadPart;
printf("Your performance counter ticks %I64u times per second\n", freq.QuadPart);
printf("Resolution is %lf nanoseconds\n", resolution*1e9);
printf("Code under test took %lf sec\n", elapsedTime);
return 0;
}
void f(long double fb[], long double fA) {
fb[0] = fb[1]* fA;
fb[1] = fb[1] - 1;
return;
}
它适用于 MVS2008 和 2003 年的 Borland C++ builderX。