C programming time time.h


In C programming the <time.h> time function record the current calendar time.It records the Year,date,day,hour,minute,seconds of the moment the function is call.The declaration of the function is given below.

time_t time(time_t *timer);

Parameters:
time -The argument which stores the current calendar time.

Return type
time_t -Returns the implementation’s best approximation to the current calendar time.If the calendar time is not available it returns (time_t)(-1).If the argument-timer,is not a pointer to NULL,the return value is also assigned to the object it points to.This simply means the returned value and the ‘timer’ content are the same.

Note the ‘time_t’ is an arithmetic type whose range and precision are defined by the implementation, and need not necessarily belong to ‘int’ type.A code example is given below.

Code example

time_t t ,ret ;

ret=time(&t); //records the current time

printf(“The time() function is called at %s” , ctime(&t));

printf(“The time() function is called at %s” , ctime(&ret));

Output,

The time() function is called at Tue Jul 18 20:46:43 2017

The time() function is called at Tue Jul 18 20:46:43 2017

The time() function here records the precise time of when it is called,the next line is prints the recorded time when the function is called.The second output is also the same since ‘ret’ contains the same value as the ‘t’ argument.

Link: C ctime

Since time() function can record the exact time when the function is called,it has many applications.For instance to see how much time a segment of code consumes to execute;an example of this is given below.





Use of time() function

Since time() can record the exact moment when it is called,one of the applications as discussed above is to record the time any segment of code consumes to execute.The usual way of doing this is to call the time() function before executing the code and call it once more after the execution of the code has ended.In this way the difference of the two time recorded will give the approximate time consume to execute the code.Using this idea let’s try to compute the time taken to find how many prime numbers exist in the range 2 to 20,000.The program is given below.

Code example

time_t t ,ret ;

bool prime=true;

long double val=20000 , count=2 ;

time(&t); //records the current time

for (int i=4 ; i<=val ; i++ )
{
 for (int i1 = 2; i1<i ; i1++)
 {
 if ( fmod(i , i1)==0.0 ) /*fmod calculate the remainder of i/i1 */
 {
 prime =false;
 }
 }

 if( prime )
 {
 count++;
 }
 prime = true;
}

printf(“The number of prime number in %Lf is %Lf”, val, count);

time(&end); //record the time when the execution ends

printf(“\nTime taken to count the number of prime numbers in %Lf is %lesec”, val, difftime(end, start)) ; //difftime compute the time difference

Output in VS,

The number of prime number in 20000.000000 is 2262.000000
Time taken to count the number of prime numbers in 20000.000000 is 2.300000e+01sec

The time taken will vary depending on the number of processors,mine is as shown above.


Related links

->C fmodl math.h

->C difftime time.h