timing - How to calculate the execution time in C? -


how can calculate execution time in following code:

#include <stdio.h>  /* core input/output operations                         */ #include <stdlib.h> /* conversions, random numbers, memory allocation, etc. */ #include <math.h>   /* common mathematical functions                        */ #include <time.h>   /* converting between various date/time formats         */ #include <sys/time.h> #define pi      3.1415926535   /* known vaue of pi                          */ #define ndarts     128   /* number of darts thrown                    */ double pseudo_random(double a, double b) { double r;  /* random number */ r = ((b - a) * ((double) rand()/(double) rand_max)) + a;  return r; } int main (int argc, char *argv[]) { int    n_procs,       /* number of processors                 */ llimit,        /* lower limit random numbers       */ ulimit,        /* upper limit random numbers       */ n_circle,      /* number of darts hit circle  */ i;             /* dummy/running index                  */ double pi_sum,        /* sum of pi values each worker    */ x,             /* x coordinate, betwen -1 & +1         */ y,             /* y coordinate, betwen -1 & +1         */ z,             /* sum of x^2 , y^2                   */ error;         /* error in calculation of pi           */ clock_t start_time,    /* wall clock - start time              */ end_time;      /* wall clock - end time                */ struct timeval stime, starttime1, endtime1; struct timeval tv1, tv2, diff;  llimit   = -1; ulimit   = 1; n_circle = 0; printf("\n  monte carlo method of finding pi\n\n"); printf("    number of processors : %d\n", n_procs); printf("    number of darts      : %d\n\n", ndarts); gettimeofday(&tv1, null); gettimeofday(&stime, null); srand(stime.tv_usec * stime.tv_usec * stime.tv_usec * stime.tv_usec); (i = 1; <= ndarts; i++) { x = pseudo_random(llimit, ulimit); y = pseudo_random(llimit, ulimit); z = pow(x, 2) + pow(y, 2); if (z <= 1.0) {    n_circle++;   } }   pi_sum = 4.0 * (double)n_circle/(double)ndarts; pi_sum = pi_sum / n_procs; error = fabs((pi_sum - pi)/pi) * 100; gettimeofday(&tv2, null); double timeval_subtract (result, x, y) { result = ((double)  x - (double) y ) / (double)clocks_per_sec; } double result1 = timeval_subtract(&diff, &tv1, &tv2); printf("    known value of  pi   : %11.10f\n", pi); printf("    average value of pi  : %11.10f\n", pi_sum); printf("    percentage error     : %10.8f\n", error); printf("    time   : \n", clock() ); printf("    start time   : \n",&tv1); printf("    end time   :\n", &tv2); printf("    time elapsed (sec)   : \n", result1 );  return 0; }  

i used timeval_subtract function , when execute code, got:

monte carlo method of finding pi  number of processors : 16372 number of darts      : 128  known value of  pi   : 3.1415926535 average value of pi  : 0.0002004184 percentage error     : 99.99362048 time   :  start time   :  end time   : time elapsed (sec)   : 

first, couldn't find mistake in finding number of processors (i must 1 processor).

second "which important point", why time, start time, end time , time elapsed empty?

because don't have adequate format strings them, need starting '%', like:

printf("    time   :%d \n", clock() ); 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -