Problem in measuring program execution time with c

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 3
Reputation: monjuri is an unknown quantity at this point 
Solved Threads: 0
monjuri monjuri is offline Offline
Newbie Poster

Problem in measuring program execution time with c

 
0
  #1
Jul 29th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Problem in measuring program execution time with c

 
0
  #2
Jul 29th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Problem in measuring program execution time with c

 
0
  #3
Jul 29th, 2008
  1. /*insertion code*/
  2.  
  3. #include <stdio.h>
  4. #include <time.h>
  5.  
  6. #define maxsize 10
  7. int A[maxsize];
  8.  
  9. int value, j, i;
  10. insertionSort ( int A[], int s )
  11. {
  12. int i, j, t;
  13. for ( i = 1; i < s; i++ )
  14. {
  15. j = i;
  16. t = A[j];
  17. while ( j > 0 && A[j-1] > t )
  18. {
  19. A[j] = A[j-1];
  20. j--;
  21. }
  22. A[j] = t;
  23. }
  24. }
  25.  
  26. void printarr ( int a )
  27. {
  28. int i;
  29. for ( i = 0; i < a; i++ )
  30. {
  31. printf ( "%d", A[i] );
  32. printf ( "\n" );
  33. }
  34. }
  35.  
  36. //Global Variables
  37. double timer;
  38. double timer2;
  39.  
  40. void startTimer()
  41. {
  42. timer = clock();
  43. }
  44.  
  45. double stopTimer()
  46. {
  47. timer2 = clock();
  48. timer = timer2 - timer;
  49. return timer;
  50. }
  51.  
  52. int main()
  53. {
  54. int i, s;
  55. double time;
  56. printf ( "enter the number of numbers to be entered \n" );
  57. scanf ( "%d", &s );
  58. for ( i = 0; i < s; i++ )
  59. {
  60. printf ( "enter the number \n" );
  61. scanf ( "%d", &A[i] );
  62. }
  63. printf ( "array before sorting " );
  64. printarr ( s );
  65. insertionSort ( A, s );
  66. printf ( "array after sorting" );
  67. printarr ( s );
  68. startTimer();
  69. time = stopTimer();
  70.  
  71. printf ( " the execution time %lf", time );
  72. getchar();
  73. getchar();
  74. }
http://faq.cprogramming.com/cgi-bin/...&id=1043284392
Last edited by iamthwee; Jul 29th, 2008 at 6:15 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Problem in measuring program execution time with c

 
0
  #4
Jul 30th, 2008
Use clock(), clock_t and CLOCKS_PER_SECOND. That way you can get the time in milliseconds, nanoseconds.. whatever you want.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Problem in measuring program execution time with c

 
0
  #5
Jul 31st, 2008
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...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC