아래 코드는 for 루프의 런타임을 초 단위로 찾아야합니다. 다른 리소스를 살펴보면 for 루프가 실행 된 후 clock()
에서 처음으로 clock()
을 빼내는 트릭을 수행해야합니다. 코드가 쓰여진 것처럼 작동하지 않는 이유는 무엇입니까?C 코드에서 런타임 코드 찾기
#include <stdio.h>
#include <time.h>
//prototypes
int rfact(int n);
int temp = 0;
main()
{
int n = 0;
int i = 0;
double result = 0.0;
clock_t t;
printf("Enter a value for n: ");
scanf("%i", &n);
printf("n=%i\n", n);
//get current time
t = clock();
//process factorial 2 million times
for(i=0; i<2000000; i++)
{
rfact(n);
}
printf("n=%i\n", n);
//get total time spent in the loop
result = (double)((clock() - t)/CLOCKS_PER_SEC);
//print result
printf("runtime=%d\n", result);
}
//factorial calculation
int rfact(int n)
{
if (n<=0)
{
return 1;
}
return n * rfact(n-1);
}
당신은 질문을 잊어 버렸습니다. –
그게 당혹 스럽네. – zakparks31191