Miraz
Miraz

Reputation: 343

Same C program but different execution time

I wrote a C program to find out the prime numbers in a given parameter. But for the same input and output, the program has different execution times.

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int n,p,k;
    scanf("%d", &n);
    int prime[n+1];
    for (p = 2; p <= n; ++p)
    {
        if (prime[p]!=-1)
        {
            for (int i = p*2,k=2; i < n; k++,i=k*p)
            {
                prime[i]=-1;
            }
        }
    }
    for (int i = 1; i < n ; ++i)
    {
        if (prime[i]!=-1)
        {
            printf("%d  ",i );
        }
    }
return 0;
}

enter image description here

enter image description here

Upvotes: 0

Views: 399

Answers (2)

Tarequzzaman Khan
Tarequzzaman Khan

Reputation: 494

The execution of a program on an environment is not dependent only on the code but on some other environment variables such as CPU load.

CLOCKS_PER_SEC is a constant which is declared in <time.h>. To get the CPU time used by a task within a C application, use:

clock_t begin = clock();

/* Do the work. */

clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;

For your program, You can probably check the

  1. CPU Time took on input.
  2. CPU Time is taken to generate Prime Number
  3. ...... and so on
#include <stdio.h>
#include<time.h>

int main(int argc, char const *argv[])
{
    int n,p,k;
    clock_t t , t1, t2; 
    t = clock(); 
    scanf("%d", &n);
    t = clock() - t; 
    double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds 
    printf("Took %f seconds to take the input\n", time_taken); 
    
    
    
    t2 = clock(); 
    int prime[n+1];
    for (p = 2; p <= n; ++p)
    {
        if (prime[p]!=-1)
        {
            for (int i = p*2,k=2; i < n; k++,i=k*p)
            {
                prime[i]=-1;
            }
        }
    }
    t2 = clock() - t2;
    double time_taken2 = ((double)t2)/CLOCKS_PER_SEC; // in seconds 
    printf("Took %f seconds for generating the prime number \n", time_taken2); 
    
  
    for (int i = 1; i < n ; ++i)
    {
        if (prime[i]!=-1)
        {
            printf("%d  ",i );
        }
    }
return 0;
}

Output:

200                                                                                                                     
Took 0.000075 seconds to take the input                                                                                 
Took 0.000004 seconds for generating the prime number                                                                   
1  2  3  5  7  11  13  17  19  23  29  31  37  41  43  47  53  59  61  67  71  73  79  83  89  97  101  103  107  109  1
13  127  131  137  139  149  151  157  163  167  173  179  181  191  193  197  199   

The time is depended on your CPU load. For details please visit here

Upvotes: 2

4386427
4386427

Reputation: 44246

What you are measuring is the time it takes the user to type the input, i.e. 200.

Calculating the result after the input is given will not take 2 to 5 sec on any modern computer.

It is true that "the computer may be doing other things" and therefore give varying execution time - but that won't give you a 3 sec increase in code like this.

To make the measurement more reliable, you need to "remove" the user input, i.e. remove the scanf.

Instead of scanf you should give the value of n as a command line argument.

Use code like

// scanf("%d", &n); Dont use scanf but use lines like the two below.
if (argc < 2) exit(1); // user error - no number given
n = atoi(argv[1]);     // convert command line arg to integer

And start the program like:

test2.exe 200

Now the measured time will be much smaller than 2-5 sec and you won't see execution time vary so much.

Note: While atoi is simple to use, it's in general better to use strtol

Upvotes: 4

Related Questions