C 试图将程序的执行时间存储到数组中,但数组元素始终显示相同的值

C 试图将程序的执行时间存储到数组中,但数组元素始终显示相同的值,c,arrays,time,C,Arrays,Time,我用C语言计算一个程序在不同输入数据下的运行时间。 我正在运行程序100次,并希望找到平均运行时间。 为了找到平均值,我想将每个运行时间值存储到一个数组中,但是存储的值总是显示相同的数字 我曾尝试将float类型更改为long和double,但没有成功。 我添加了一个print语句来显示这些值,它们是我想要的值,只是在插入数组后会被更改 // C programming int main(){ clock_t t; int n, m = 0; int arr_time[100]; // als

我用C语言计算一个程序在不同输入数据下的运行时间。 我正在运行程序100次,并希望找到平均运行时间。 为了找到平均值,我想将每个运行时间值存储到一个数组中,但是存储的值总是显示相同的数字

我曾尝试将float类型更改为long和double,但没有成功。 我添加了一个print语句来显示这些值,它们是我想要的值,只是在插入数组后会被更改

// C programming
int main(){

clock_t t;
int n, m = 0;
int arr_time[100]; // also tried changing the array type from int to float
int repeat, repeat_times = 1000;

for(n=1;n<100;++n)
{
    t = clock();
    for (repeat=0;repeat<repeat_times; ++repeat)
    {
        some_function();
    }

    t = clock()-t;
    printf("%f", ((float)t)/CLOCKS_PER_SEC/repeat_times); // to check the values to be stored
    arr_time[m] = ((float)t)/CLOCKS_PER_SEC/repeat_times; // values stored are not the same as previous line though
    m+=1;
    printf("It took %f seconds to run some_function(%d)\n",
           ((float)t)/CLOCKS_PER_SEC/repeat_times,n);
}

int sum = 0;
for (n=1;n<100;++n)
{
    sum += arr_time[n];
}
for (n=0;n<10;n++)
{
    printf("(%f)\n", arr[n]); // checking which values are stored into the array
}

printf("(%f)\n", sum);
int average = ((float)sum)/99;
printf("The average time in seconds to run some_function is (%f)\n", average);

return 0;
}
//C编程
int main(){
时钟;
int n,m=0;
int arr_time[100];//还尝试将数组类型从int更改为float
int repeat,repeat_times=1000;
对于(n=1;n
/。。。
arr_time[m]=((float)t)/每秒时钟数/重复次数;
//您可以将它们存储在arr[]为float的位置
arr[n]=到达时间[m]
//

对于(n=0;n在调试器中单步执行代码显示了什么?明白了!浮点需要存储在类型为的数组中double@Camilla,浮点需要存储在浮点数组中。它不必是双精度的。是的,浮点和双精度类型都可以工作
//...
arr_time[m] = ((float)t)/CLOCKS_PER_SEC/repeat_times;
//you can store them here where arr[] is float    
arr[n] = arr_time[m]
//
for (n=0;n<10;n++)
{
// or use arr_time[] and not arr[] because you are not using arr[] in your program
printf("(%f)\n", arr_time[n]); // checking which values are stored into the array
}