C执行时间
C execution Time
当我想在 C 中使用 time.h 检查我的执行时间时遇到问题。即;我添加了一个计时器来计算开始搜索时的执行时间。但是,当我 select 多个变量时,计时器开始计时。
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
int selection = 0;
printf(" 1. Calculate Execution Time Depends on Variables. \n 2. Calculate Variable Numbers Depends on Time Execution.\n What You want to do ? \n Selection : ");
scanf_s("%d", &selection);
if (selection==1)
{
int numberselection = 0;
int number_Elements = 0;
printf(" Please Select the Number of Elements. \n 1. 500.000 2. 1.000.000 3. 2.000.000 4. 3.000.000 5. 100.000.000 \n Selection : ");
scanf_s("%d", &numberselection);
if (numberselection==1)
{
number_Elements = 500000;
}
if (numberselection == 2)
{
number_Elements = 1000000;
}
if (numberselection == 3)
{
number_Elements = 2000000;
}
if (numberselection == 4)
{
number_Elements = 3000000;
}
if (numberselection == 5)
{
number_Elements = 100000000;
}
int i = 0;
int *NumberList = malloc(number_Elements * sizeof(int));
NumberList[number_Elements];
srand((unsigned)time(NULL));
for (i = 0; i < number_Elements; i++)
{
double random = rand() % 99999 + 99999999;
NumberList[i] = random;
}
double number_Search;
printf("\n Please Enter Number To Search : ");
scanf_s("%d", &number_Search);
Algorithm(number_Elements, NumberList, number_Search);
}
return 0;
}
int Algorithm(int G_Number_Elements , int G_NumberList[] , int G_Number_Search)
{
int k = 0;
double time_taken = 0;
clock_t t = 0;
for ( k = 0; k < G_Number_Elements; k++) // NUMBER TO TIME
{
if (G_NumberList[k] == G_Number_Search)
{
break;
}
else
{
printf("\n Not Found...");
break;
}
}
t = clock() - t;
time_taken = ((double)t) / CLOCKS_PER_SEC;
printf("\n Exacution Time Depends %d Variables = %f ms.\n", G_Number_Elements, time_taken);
return 0;
}
当我得到定时器值时,我等待控制台的时间被添加了。我有两种情况;
Pictures of Scenarios
我的问题是,如何消除计时器的等待时间?
问题是 clock()
计算您的程序使用的所有时钟滴答。
您可能想像这样
将 t
初始化为 clock()
的当前值
clock_t t = clock();
这样您就可以将选择输入所花费的时间从时间计算中扣除。
从 clock()
的 C reference 获取的其他信息
To calculate the actual processing time of a program, the value returned by clock shall be compared to a value returned by a previous call to the same function.
我在@Korrat 的帮助下解决了问题!
代码的最后状态:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
int selection = 0;
printf(" 1. Calculate Execution Time Depends on Variables. \n 2. Calculate Variable Numbers Depends on Time Execution.\n \n What You Want to Do ? \n Selection : ");
scanf_s("%d", &selection);
if (selection == 1)
{
int numberselection = 0;
int number_Elements = 0;
printf(" Please Select the Number of Elements. \n 1. 500.000 2. 1.000.000 3. 2.000.000 4. 3.000.000 5. 100.000.000 \n Selection : ");
scanf_s("%d", &numberselection);
if (numberselection == 1)
{
number_Elements = 500000;
}
if (numberselection == 2)
{
number_Elements = 1000000;
}
if (numberselection == 3)
{
number_Elements = 2000000;
}
if (numberselection == 4)
{
number_Elements = 3000000;
}
if (numberselection == 5)
{
number_Elements = 100000000;
}
int i = 0;
int *NumberList = malloc(number_Elements * sizeof(int));
NumberList[number_Elements];
srand((unsigned)time(NULL));
for (i = 0; i < number_Elements; i++)
{
double random = rand() % 99999 + 99999999;
NumberList[i] = random;
}
double number_Search;
printf("\n Please Enter Number To Search : ");
scanf_s("%d", &number_Search);
int k = 0;
clock_t begin = clock();
for (k = 0; k <= number_Elements; k++) // NUMBER TO TIME
{
if (NumberList[k] == number_Search)
{
break;
}
if (k == number_Elements)
{
printf("Your Number Not Found in List ! \n");
}
}
clock_t end = clock();
float time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("\n Exacution Time Depends %d Variables = %f Second !. \n", number_Elements, time_spent);
}
return 0;
}
当我想在 C 中使用 time.h 检查我的执行时间时遇到问题。即;我添加了一个计时器来计算开始搜索时的执行时间。但是,当我 select 多个变量时,计时器开始计时。
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
int selection = 0;
printf(" 1. Calculate Execution Time Depends on Variables. \n 2. Calculate Variable Numbers Depends on Time Execution.\n What You want to do ? \n Selection : ");
scanf_s("%d", &selection);
if (selection==1)
{
int numberselection = 0;
int number_Elements = 0;
printf(" Please Select the Number of Elements. \n 1. 500.000 2. 1.000.000 3. 2.000.000 4. 3.000.000 5. 100.000.000 \n Selection : ");
scanf_s("%d", &numberselection);
if (numberselection==1)
{
number_Elements = 500000;
}
if (numberselection == 2)
{
number_Elements = 1000000;
}
if (numberselection == 3)
{
number_Elements = 2000000;
}
if (numberselection == 4)
{
number_Elements = 3000000;
}
if (numberselection == 5)
{
number_Elements = 100000000;
}
int i = 0;
int *NumberList = malloc(number_Elements * sizeof(int));
NumberList[number_Elements];
srand((unsigned)time(NULL));
for (i = 0; i < number_Elements; i++)
{
double random = rand() % 99999 + 99999999;
NumberList[i] = random;
}
double number_Search;
printf("\n Please Enter Number To Search : ");
scanf_s("%d", &number_Search);
Algorithm(number_Elements, NumberList, number_Search);
}
return 0;
}
int Algorithm(int G_Number_Elements , int G_NumberList[] , int G_Number_Search)
{
int k = 0;
double time_taken = 0;
clock_t t = 0;
for ( k = 0; k < G_Number_Elements; k++) // NUMBER TO TIME
{
if (G_NumberList[k] == G_Number_Search)
{
break;
}
else
{
printf("\n Not Found...");
break;
}
}
t = clock() - t;
time_taken = ((double)t) / CLOCKS_PER_SEC;
printf("\n Exacution Time Depends %d Variables = %f ms.\n", G_Number_Elements, time_taken);
return 0;
}
当我得到定时器值时,我等待控制台的时间被添加了。我有两种情况;
Pictures of Scenarios
我的问题是,如何消除计时器的等待时间?
问题是 clock()
计算您的程序使用的所有时钟滴答。
您可能想像这样
t
初始化为 clock()
的当前值
clock_t t = clock();
这样您就可以将选择输入所花费的时间从时间计算中扣除。
从 clock()
To calculate the actual processing time of a program, the value returned by clock shall be compared to a value returned by a previous call to the same function.
我在@Korrat 的帮助下解决了问题!
代码的最后状态:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
int selection = 0;
printf(" 1. Calculate Execution Time Depends on Variables. \n 2. Calculate Variable Numbers Depends on Time Execution.\n \n What You Want to Do ? \n Selection : ");
scanf_s("%d", &selection);
if (selection == 1)
{
int numberselection = 0;
int number_Elements = 0;
printf(" Please Select the Number of Elements. \n 1. 500.000 2. 1.000.000 3. 2.000.000 4. 3.000.000 5. 100.000.000 \n Selection : ");
scanf_s("%d", &numberselection);
if (numberselection == 1)
{
number_Elements = 500000;
}
if (numberselection == 2)
{
number_Elements = 1000000;
}
if (numberselection == 3)
{
number_Elements = 2000000;
}
if (numberselection == 4)
{
number_Elements = 3000000;
}
if (numberselection == 5)
{
number_Elements = 100000000;
}
int i = 0;
int *NumberList = malloc(number_Elements * sizeof(int));
NumberList[number_Elements];
srand((unsigned)time(NULL));
for (i = 0; i < number_Elements; i++)
{
double random = rand() % 99999 + 99999999;
NumberList[i] = random;
}
double number_Search;
printf("\n Please Enter Number To Search : ");
scanf_s("%d", &number_Search);
int k = 0;
clock_t begin = clock();
for (k = 0; k <= number_Elements; k++) // NUMBER TO TIME
{
if (NumberList[k] == number_Search)
{
break;
}
if (k == number_Elements)
{
printf("Your Number Not Found in List ! \n");
}
}
clock_t end = clock();
float time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("\n Exacution Time Depends %d Variables = %f Second !. \n", number_Elements, time_spent);
}
return 0;
}