facebook like button

Showing posts with label getting running time. Show all posts
Showing posts with label getting running time. Show all posts

23 November, 2011

Get the execution time of a C program

The need:
     Need is same as previous program which gives you precision of nanoseconds but that program would work only on linux. This program will work on windows also but will give you precision of milli seconds only.  All you need to do is to place your test program code (the code whose execution time is to be measured) in between the comments which indicate the start and end of the program.
The code:
----------------------------------------------------------
/*This program should work on both windows and linux*/
#include<time.h> 
#include<stdio.h>
#include<stdlib.h> 
#define START if ( (start_time = clock()) == -1) {printf("Could not call clock");exit(1);} 
#define STOP if ( (stop_time = clock()) == -1) {printf("Could call clock");exit(1);} 
#define PRINT_TIME_DIFF printf( "Your program took %.3f seconds.\n", ((double)stop_time-start_time)/CLOCKS_PER_SEC); 
    
int main()
{ 
    clock_t start_time, stop_time;
    int i=0; 
    START
    //test program code begins
    
    //test program code ends 
    STOP 
    PRINT_TIME_DIFF
    return 0; 
} 
----------------------------------------------------------
Approach:
   The approach is simple. There is a built-in function clock() in <time.h> header file. In this program I have use macros to differentiate between your test code and the statement which would be used to print the time and other things. I thought this would simplify the look of the program a little bit.
Remarks:
1. This program should work on both windows and linux.
2. This program will give accuracy of (1/1000)th part of a second.

22 November, 2011

Getting running time of a C program

The need:
     Need is obvious.Whenever you are curious to find running time of a program or you are asked to do so, you can use this program. All you need to do is to place your program code(the code between the brackets of main() function ) in between the comments which indicate the start and end of the program.
The code:
----------------------------------------------------------
/*This program is intended to work on linux*/
#include <stdio.h> 
#include <time.h> 
void print_time_diff(struct timespec begin,struct timespec end);
int main ( void )
{
    struct timespec start, end;
    gettimeofday(&start, NULL);
    //program code begins
    //program code ends
    gettimeofday(&end, NULL);
    print_time_diff(start,end);
    return 0;
}

void print_time_diff(struct timespec begin,struct timespec end)
{
    unsigned int sec,nanosec;
    if (end.tv_nsec<begin.tv_nsec) {
        sec = end.tv_sec-begin.tv_sec-1;
        nanosec = 1000000000+end.tv_nsec-begin.tv_nsec;
    } else {
        sec = end.tv_sec-begin.tv_sec;
        nanosec = end.tv_nsec-begin.tv_nsec;
    }
    printf("your program took %d seconds and %u nanoseconds to run.\n",sec,nanosec);
} 
----------------------------------------------------------
Approach:
   The approach is simple. There is a built-in structure timespec in <time.h> header file of linux.The built-in definition of structure is:
struct timespec
  {
    __time_t tv_sec;        /* Seconds.  */
    long int tv_nsec;        /* Nanoseconds.  */
  };
The program declares 2 struct variables start and end. built-in function gettimeofday() fills the struct variable passed to it with suitable values at that particular instance. This implies that start gets the value of time when your program (code to be measured) is just about to start and end gets the value of time as soon as your program finishes.
print_time_diff() is user defined function which prints the difference of the two times passed to it as arguments.
Remarks:
1. This program should not work on windows and operating systems other than linux.
2. The program which would work on windows will be posted shortly.