hello everyone.
i m monjuri. i want to find out the execution time of a C program. it works fine. but i want the time in milisecond or microsecond. Can anybody help me???

here is my code:

/*insertion code*/

#include <stdio.h>
#include <time.h>


#define maxsize 10
int A[maxsize];




int value,j,i;
insertionSort(int A[],int s){
        int i, j, t;
        for (i=1; i<s; i++)
        {
            j=i;
            t=A[j];
            while (j>0 && A[j-1]>t)
            {
                A[j]=A[j-1];
                j--;
            }
            A[j]=t;
        }
    }




void printarr(int a)
{
  int i;
  for(i=0;i<a;i++)
  {

  printf("%d",A[i]);
  printf("\n");
  }
  }



//Global Variables
double timer;
double timer2;

void startTimer()
{
timer = clock();
}

double stopTimer()
{
timer2 = clock();
timer = timer2 - timer;
return timer;
}

main()
{
  int i,s;
  double time;
  printf("enter the number of numbers to be entered \n");
  scanf("%d",&s);
  for(i=0;i<s;i++)
  {
     printf("enter the number \n" );
     scanf("%d",&A[i]);
     }
  printf("array before sorting ");
  printarr(s);
 insertionSort(A,s);
  printf("array after sorting");
  printarr(s);


startTimer();
time= stopTimer();

printf(" the execution time %lf",time);
  }

Thank u.

Remember seeing this?

Our Software Development forum category encompasses topics related to application programming and software design. When posting programming code, encase it in code, code=syntax, where 'syntax' is any language found within our Code Snippets section, or icode, for inline code, bbcode tags. Also, to keep DaniWeb a student-friendly place to learn, don't expect quick solutions to your homework. We'll help you get started and exchange algorithm ideas, but only if you show that you're willing to put in effort as well.

What about this?
Read This Before Posting

Member Avatar for iamthwee
/*insertion code*/

#include <stdio.h>
#include <time.h>
 
#define maxsize 10
int A[maxsize]; 

int value, j, i;
insertionSort ( int A[], int s )
{
  int i, j, t;
  for ( i = 1; i < s; i++ )
  {
    j = i;
    t = A[j];
    while ( j > 0 && A[j-1] > t )
    {
      A[j] = A[j-1];
      j--;
    }
    A[j] = t;
  }
} 

void printarr ( int a )
{
  int i;
  for ( i = 0; i < a; i++ )
  {
    printf ( "%d", A[i] );
    printf ( "\n" );
  }
} 

//Global Variables
double timer;
double timer2;

void startTimer()
{
  timer = clock();
}

double stopTimer()
{
  timer2 = clock();
  timer = timer2 - timer;
  return timer;
}

int main()
{
  int i, s;
  double time;
  printf ( "enter the number of numbers to be entered \n" );
  scanf ( "%d", &s );
  for ( i = 0; i < s; i++ )
  {
    printf ( "enter the number \n" );
    scanf ( "%d", &A[i] );
  }
  printf ( "array before sorting " );
  printarr ( s );
  insertionSort ( A, s );
  printf ( "array after sorting" );
  printarr ( s ); 
  startTimer();
  time = stopTimer();

  printf ( " the execution time %lf", time );
  getchar();
  getchar();
}

http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1048385167&id=1043284392

Use clock(), clock_t and CLOCKS_PER_SECOND. That way you can get the time in milliseconds, nanoseconds.. whatever you want. ;)

Regrettably, in practice no way to measure time intervals with better than ~15 MILLIsecond precision in platform-independent manner (i.e with C RTL functions including clock()). The CLOCKS_PER_SECOND macros per se does not guarantee that your program may catch every tick with this frequency.

I don't know what's your target platform. For Windows I have my own CPU performance counter based C++ code to measure time intervals with ~1.5 MICROseconds precision (on Windows XP Pro installation). It works perfectly and it's so easy to adopt it in C... but come back to the 1st statement of this paragraph...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.