Search code examples
ctimetimerexecution

C execution Time


I have a problem when I want to check my execution time with time.h in C. Namely; I added a timer for calculation of execution time when a search is started. However, the timer is starting when I select a number of variables.

#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;
}

The time I wait for the console is added when I get the timer value. I have a two scenario;

Pictures of Scenarios

My question is that, How can I eliminate the waiting time from the timer?


Solution

  • The problem is that clock() counts all clock ticks your program uses. You probably want to initialize t to the current value of clock() like this

    clock_t t = clock();
    

    That way you take the amount of time spent on selecting your input out of the time calculation.

    Additinal information taken from the C reference on 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.