vendredi 29 mai 2015

How do i measure the running time of my program in C?

I have written this program in C that prints all the prime numbers til the one read at the top of the program. This is the program:

int i,j;
bool *p;
int g;
printf("inserisca la grandezza della tabella: ");
scanf("%i",&g);
wait(1);
p=(bool *)malloc(g*sizeof(bool));
    for(i=0;i<g+1;i++){
        p[i]=true;
    }
i=2;
while(i<=(g-2)){
    while(!p[i]){
        i++;
    }
    j=i+i;
    while(j<=(g-1)){
        p[j]=false;
        j=j+i;
    }
    i++;
}
j=0;
for(i=1;i<g;i++){
    if(p[i]!=false){
        printf("\t%i\n",i);
        j++;
    }
}
printf("sono stati trovati %i numeri primi\n",j);
free(p);

My problem is i want to know how much milliseconds or nanoseconds it takes the program to allocate the memory and do the calculations? Have i to include some libraries like time.h? Please don't mind the lenguage i'm an italian student.

Aucun commentaire:

Enregistrer un commentaire