gpt4 book ai didi

c - 使用 time.h 的奇怪 CLOCKS_PER_SEC 值

转载 作者:行者123 更新时间:2023-11-30 19:10:07 26 4
gpt4 key购买 nike

目前,我正在尝试对一个过程进行计时,以与我在网上找到的使用 opencl 的示例程序进行比较。然而,当我尝试计算此过程的时间时,我会得到非常奇怪的值,如下所示。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <CL/cl.h>
#include <time.h>

int main(void) {

int n = 100000;
size_t bytes = n * sizeof(double);

double *h_a;
double *h_b;
double *h_c;
h_a = (double*)malloc(bytes);
h_b = (double*)malloc(bytes);
h_c = (double*)malloc(bytes);

int i;
for(i = 0; i < n; i++)
{
h_a[i] = sinf(i)*sinf(i);
h_b[i] = cosf(i)*cosf(i);
}

clock_t start = clock();
for(i = 0; i < n; i++)
h_c[i] = h_a[i] + h_b[i];
clock_t stop = clock();
double time = (stop - start) / CLOCKS_PER_SEC;
printf("Clocks per Second: %E\n", CLOCKS_PER_SEC);
printf("Clocks Taken: %E\n", stop - start);
printf("Time Taken: %E\n", time);

free(h_a);
free(h_b);
free(h_c);

system("PAUSE");
return 0;
}

结果:

    C:\MinGW\Work>systesttime
Clocks per Second: 1.788208E-307
Clocks Taken: 1.788208E-307
Time Taken: 0.000000E+000
Press any key to continue . . .

它为那里的一切赋予了非常奇怪的值(value)。我知道它必须在 1,000,000 左右,但我不知道为什么这样做。它曾经为同样令人担忧的所有事物给出大约 6E+256 的值。

最佳答案

看起来您的clock_t不是 double 的,因此%E是错误的格式说明符。

可能很长。试试这个:

printf("Clocks per Second: %E\n", (double)CLOCKS_PER_SEC);

关于c - 使用 time.h 的奇怪 CLOCKS_PER_SEC 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41870739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com