编辑:我已经弄明白了。第二个算法运行得如此高效,以至于输入 < 100,000 时甚至都没有记录时间
我正在尝试测量我在函数中实现的某个算法需要多长时间才能执行。我包括了<time.h>
我围绕着 time_t
的功能变量。它非常适合我的第一次实现,但不适用于我的第二次实现。
我是否需要在两次使用之间关闭时钟流(想不出更好的工作)?有点像你如何关闭 Scanner
在 Java 程序中流式传输。这是我的代码,以防我没有很好地解释它。
switch(choice) {
case 1:
printf("Beginning prefixAverages1\n");
clock_t begin1 = clock();
int *a1 = prefixAverages1(input);
clock_t end1 = clock();
double time_spent1 = (double)(end1 - begin1) * 1000.0 / CLOCKS_PER_SEC;
free(a1);
printf("Algorithm took %f milliseconds to execute \n", time_spent1);
break;
case 2:
printf("Beginning prefixAverages2\n");
clock_t begin2 = clock();
int *a2 = prefixAverages2(input);
clock_t end2 = clock();
double time_spent2 = (double)(end2 - begin2) * 1000.0 / CLOCKS_PER_SEC;
free(a2);
printf("Algorithm took %f milliseconds to execute \n", time_spent2);
break;
default:
printf("Invalid input!");
break;
}
在我的第一种情况下时间显示正确,但在第二种情况下则不正确。我已尝试进行一些研究,但找不到任何特别适合我的场景的内容。
在运行案例 1 时,根据输入,我得到了 600-1000 毫秒的运行时间(听起来不错)。当我运行案例 2 时,无论输入如何,我都会得到 00.000
如果有帮助,这是我的功能:
int* prefixAverages1(int input) {
int x[input];
int *a = malloc(input*sizeof(*a));
srand(time(NULL));
for(int i = 0; i < input; i++) {
int sum = 0;
for(int j = 0; j < i; j++) {
int r = rand() % 100;
x[j] = r;
sum = sum + x[j];
}
a[i] = sum / (i+1);
}
return a;
}
int* prefixAverages2(int input) {
int sum = 0;
int x[input];
int *a = malloc(input*sizeof(*a));
srand(time(NULL));
for(int i = 0; i < input; i++) {
int r = rand() % 100;
x[i] = r;
sum = sum + x[i];
a[i] = sum / (i+1);
}
return a;
}
最佳答案
虽然我不知道为什么第二个选择可能是 0,但由于这两个函数具有相同的签名,您可以通过使用函数指针来消除冗余代码。
void do_benchmark( const char *name, int*(*functionPtr)(int), int input ) {
printf("Beginning %s\n", name);
clock_t begin = clock();
int *ret = (*functionPtr)(input);
clock_t end = clock();
double time_spent = (double)(end - begin) * 1000.0 / CLOCKS_PER_SEC;
free(ret);
printf("Algorithm took %f milliseconds to execute \n", time_spent);
}
然后两个函数都使用相同的计时代码运行,消除基准测试代码中的差异是罪魁祸首。
switch(choice) {
case 1:
do_benchmark("prefixAverages1", &prefixAverages1, input);
break;
case 2:
do_benchmark("prefixAverages2", &prefixAverages2, input);
break;
default:
printf("Invalid input!");
break;
}
请注意 clock
可能会失败。
If the processor time used is not available or its value cannot be represented, the function returns the value (clock_t)(-1).
您需要检查该故障。
if( begin == (clock_t)-1 ) {
fprintf(stderr, "Begin time not available.\n");
}
else if( end == (clock_t)-1 ) {
fprintf(stderr, "End time not available.\n");
}
else {
double time_spent = (double)(end - begin) * 1000.0 / CLOCKS_PER_SEC;
printf("Algorithm took %f milliseconds to execute \n", time_spent);
}
关于c - 测量执行函数所花费的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50478119/